在我的Android应用程序中,我需要为一组按钮应用样式,而不是为每个按钮设置样式。像这样:
<?xml version="1.0" encoding="utf-8"?>
<!-- put here style="@ststyle/Button_Style" -->
<Button android:id="@+id/button1" android:text="@string/b01" />
<Button android:id="@+id/button2" android:text="@string/b02" />
<Button android:id="@+id/button4" android:text="@string/b03" />
<!-- end style -->
答案 0 :(得分:3)
您可以像这样编写按钮样式;
<style name="style_btn" parent="Wrap">
<item name="android:background">@drawable/btn_bg</item>
<item name="android:gravity">center</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginTop">4dp</item>
<item name="android:minWidth">90dp</item>
</style>
<Button
android:id="@+id/attach_file"
style="@style/style_btn"
android:layout_centerVertical="true"
android:background="@drawable/orange_bg"
android:drawablePadding="10dp"
android:drawableRight="@drawable/attach"
android:text="@string/str_email_attach" />
答案 1 :(得分:3)
如果您需要应用程序中所有按钮的样式,请在App主题样式中提及,然后无需申请每个按钮。
<style name="YourTheme" parent="android:Theme.Light">
<item name="android:buttonStyle">@style/Button</item>
</style>
如果您需要样式按钮,则应用于每个按钮
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Button"
android:text="Button" />
答案 2 :(得分:0)
我知道我很晚才加入聚会,但在我试图找出同样的问题时,我偶然发现了这一点。
我做的是:
根据您拥有的按钮组的方式(例如3个),您需要子类按钮并创建三个自定义按钮类(见下文)
//Custom button 1
public class CustomButton1 extends Button {
public CustomButton1(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle1);
}
public CustomButton1(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle1);
}
}
//Custom button 2
public class CustomButton2 extends Button {
public CustomButton2(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle2);
}
public CustomButton2(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle2);
}
}
//Custom button 3
public class CustomButton3 extends Button {
public CustomButton3(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle3);
}
public CustomButton3(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle3);
}
}
您可以从我传递自定义attr的自定义类中看到。这些我在styles.xml中定义并使用它们作为参考。请参阅下面的styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="theme1">
<item name="@attr/attrStyle1">@style/CustomButton1</item>
<item name="@attr/attrStyle2">@style/CustomButton2</item>
<item name="@attr/attrStyle3">@style/CustomButton3</item>
<item name="android:background">@color/warning_yellow_colour</item>
</style>
<style name="CustomButton1" parent = "@android:style/Widget.Button">
<item name="android:textColor">@color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">@color/banner_background_sensor_colour</item>
</style>
<style name="CustomButton2" parent = "@android:style/Widget.Button">
<item name="android:textColor">@color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">@color/button_red_colour</item>
</style>
<style name="CustomButton3" parent = "@android:style/Widget.Button">
<item name="android:textColor">@color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">@color/text_blue_colour</item>
</style>
<attr name="attrStyle1" format="reference"/>
<attr name="attrStyle2" format="reference"/>
<attr name="attrStyle3" format="reference"/>
通过将样式链接到attr,然后将该样式应用于您的自定义类,然后您可以根据需要复制该样式
CustomButton1 theme1 = (CustomButton1)findViewById(R.id.theme1);
CustomButton2 theme2 = (CustomButton2)findViewById(R.id.theme2);
CustomButton3 theme3 = (CustomButton3)findViewById(R.id.theme3);
希望这对某人有益!