How to set R.style in a class on Android?
I know that we can set the custom style on Android layout.xml when we are creating the view.
However, I don't know why Android does not have a method to set style... Although I know the view constructor can set the style, I don't know what i开发者_如何学JAVAs the AttributeSet...
For example, I have a styles.xml
<style name="button">
<!-- All -->
<item name="android:layout_width">64dip</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_marginBottom">1dip</item>
<item name="android:layout_marginLeft">0dip</item>
<item name="android:layout_marginRight">0dip</item>
<item name="android:layout_marginTop">1dip</item>
<item name="android:background">@color/button_background_color_list</item>
<!-- Image -->
<item name="android:button">@null</item>
<item name="android:drawablePadding">-5dip</item>
<!-- Text -->
<item name="android:textColor">@color/button_label_color_list</item>
<item name="android:textSize">9dip</item>
<item name="android:textStyle">normal</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:padding">0dip</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">marquee</item>
<item name="android:marqueeRepeatLimit">marquee_forever</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
</style>
<!-- RadioButton 1 -->
<style name="button_1" parent="@style/button">
<item name="android:drawableTop">@drawable/button_1_selector</item>
<item name="android:text">One</item>
</style>
<!-- RadioButton 2 -->
<style name="button_2" parent="@style/button">
<item name="android:drawableTop">@drawable/button_2_selector</item>
<item name="android:text">Two</item>
</style>
and I can use the style at layout.xml like this,
<RadioButton android:id="@+id/radiobutton_1"
style="@style/button_1"/>
<RadioButton android:id="@+id/radiobutton_2"
style="@style/button_2"/>
But I cannot use style at a class like,
RadioButton rb = new RadioButton(this);
rb.setStyle(R.style.button_1);
Could any one help me??
Finally, I get a solution to fix it..., but it is not a good solution.
I create a custom_layout.xml,
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_button_1"
style="@style/button_1">
</Button>
And Create it in a class,
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.custom_layout, null);
Button rb = (Button)view.findViewById(R.id.custom_button_1);
Then, I can use my own style.
You can change whole theme by applying setTheme(R.style.your_theme) before you set your content view.
refere this link
http://mrbool.com/how-to-change-the-layout-theme-of-an-android-application/25837
You should read up on Applying Styles and Themes.
A "style" is basically a bunch of tags that are being passed to you through the AttributeSet, so if your style contains something like android:typeface="simple"
, you can extract that from the AttributeSet
.
There are a lot of standard Android attributes (these are specified in the docs in "XML Attributes" for View and its subclasses), which are processed by the View itself, and you can define your own.
If you are creating a view yourself rather than by inflating a layout XML, you need to create the AttributeSet yourself (for example via Xml.asAttributeSet()
).
精彩评论