2016-07-19 15 views
1

Lassen Sie uns sagen, ich habe dieses Layout xml (blaue Farbe Schaltfläche mit Welligkeit aufgrund selectableItemBackground):Android - Was entspricht "android: theme =" in Java?

<LinearLayout 
    android:id="@+id/yes_frame" 
    android:background="@color/Blue" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
    <Button 
     android:id="@+id/dialogCancelButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Cancel" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/White" 
     android:background="?attr/selectableItemBackground" 
     android:theme="@style/ripplePressed" 
    /> 
</LinearLayout> 

Und themes.xml (Ripple Farbe als weiß zu machen):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="ripplePressed"> 
     <item name="android:colorControlHighlight">@color/White</item> 
    </style> 
</resources> 

simulieren xml in Java:

CustomButton submit = new CustomButton(context); 

    LinearLayout wrapperLinearLayout = new LinearLayout(context); 
    wrapperLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT)); 
    wrapperLinearLayout.setBackgroundColor(Color.BLUE); 
    LinearLayout.LayoutParams layoutParamsButton = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      (int) Utils.convertDpToPixel(55, context)); 

    //selectableItemBackground in Java 
    int[] attrs = new int[]{R.attr.selectableItemBackground}; 
    TypedArray ta = context.obtainStyledAttributes(attrs); 
    int backgroundResource = ta.getResourceId(0, 0); 
    submit.setBackgroundResource(backgroundResource); 
    ta.recycle(); 

    submit.setLayoutParams(layoutParamsButton); 
    submit.setTextColor(Color.WHITE); 
    submit.setText("some text"); 

    wrapperLinearLayout.addView(submit); 

Die Java-co de funktioniert gut, außer ich kann nicht das Äquivalent android:theme="@style/ripplePressed in Java herausfinden.

Antwort

1

Ich denke, wenn Sie eine ContextThemeWrapper verwenden, um Ihre Ansicht zu erstellen, dann würden Sie das angegebene Thema verwenden.

Context themedContext = new ContextThemeWrapper(context, R.style.orange_theme); 
CustomButton submit = new CustomButton(themedContext); 
1

können Sie verwenden ContextThemeWrapper Klasse

In docs

Verwandte Themen