【发布时间】:2011-01-02 05:47:58
【问题描述】:
我使用以下代码从 XML 中获取视图:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
我想为按钮设置一个“样式”,我该如何在 java 中做到这一点,因为我想为我将使用的每个按钮使用多种样式。
【问题讨论】:
我使用以下代码从 XML 中获取视图:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
我想为按钮设置一个“样式”,我该如何在 java 中做到这一点,因为我想为我将使用的每个按钮使用多种样式。
【问题讨论】:
首先,您不需要使用布局充气器来创建简单的 Button。您可以使用:
button = new Button(context);
如果您想为按钮设置样式,您有 2 种选择:最简单的一种是仅指定代码中的所有元素,就像许多其他答案所建议的那样:
button.setTextColor(Color.RED);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
另一个选项是在 XML 中定义样式,并将其应用于按钮。在一般情况下,您可以为此使用ContextThemeWrapper:
ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
button = new Button(newContext);
要更改 TextView(或其子类,如 Button)上的文本相关属性,有一个特殊的方法:
button.setTextAppearance(R.style.MyTextStyle);
或者,如果您需要支持 API-23 (Android 6.0) 之前的设备
button.setTextAppearance(context, R.style.MyTextStyle);
此方法不能用于更改所有属性;例如,要更改填充,您需要使用ContextThemeWrapper。但是对于文字颜色、大小等可以使用setTextAppearance。
【讨论】:
ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle); button = new Button(newContext); 假设在按钮创建时选择样式。如果创建按钮后需要更改样式怎么办?
通常您不能以编程方式更改样式;您可以使用themes or styles 在 XML 布局中设置屏幕、布局的一部分或单个按钮的外观。但是,主题可以be applied programmatically。
还有像StateListDrawable 这样的东西,它可以让你为Button 可以处于的每个状态定义不同的drawable,无论是聚焦、选择、按下、禁用等等。
例如,要让您的按钮在按下时改变颜色,您可以定义一个名为 res/drawable/my_button.xml 的 XML 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/btn_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/btn_normal" />
</selector>
然后,您可以通过设置属性android:background="@drawable/my_button" 将此选择器应用于Button。
【讨论】:
style 属性,但您可以像使用任何其他视图一样以编程方式设置 Button 的背景,如果这足够的话。此外,作为Button 继承自TextView,您可以更改文本属性。只需查看这些项目的 API 文档...developer.android.com/reference/android/view/…
是的,例如,您可以在按钮中使用
Button b = new Button(this);
b.setBackgroundResource(R.drawable.selector_test);
【讨论】:
你可以像这样做样式属性:
Button myButton = new Button(this, null,android.R.attr.buttonBarButtonStyle);
代替:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
style="?android:attr/buttonBarButtonStyle"
/>
【讨论】:
如果您使用的是 Support 库,您可以简单地使用
TextViewCompat.setTextAppearance(textView, R.style.AppTheme_TextStyle_ButtonDefault_Whatever);
用于文本视图和按钮。其余视图也有类似的类:-)
【讨论】:
根据您想要更改的样式属性,您可以使用 Paris 库:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
Paris.style(view).apply(R.style.YourStyle);
支持许多属性,如背景、填充、textSize、textColor 等。
免责声明:我创作了这个库。
【讨论】:
@Dayerman 和@h_rules 的答案是正确的。 用代码给出一个详细的例子, 在 drawable 文件夹中,创建一个名为 button_disabled.xml 的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="@color/silver"/>
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>
然后在 Java 中,
((Button) findViewById(R.id.my_button)).setEnabled(false);
((Button) findViewById(R.id.my_button)).setBackgroundResource(R.drawable.button_disabled);
这会将按钮的属性设置为禁用并将颜色设置为银色。
[颜色在color.xml中定义为:
<resources>
<color name="silver">#C0C0C0</color>
</resources>
【讨论】:
对于任何寻找材料答案的人,请参阅此 SO 帖子:Coloring Buttons in Android with Material Design and AppCompat
我结合使用这个答案将按钮的默认文本颜色设置为白色: https://stackoverflow.com/a/32238489/3075340
然后这个答案https://stackoverflow.com/a/34355919/3075340 以编程方式设置背景颜色。代码是:
ViewCompat.setBackgroundTintList(your_colored_button,
ContextCompat.getColorStateList(getContext(),R.color.your_custom_color));
your_colored_button 可以只是一个普通的Button 或 AppCompat 按钮,如果你愿意的话 - 我用这两种类型的按钮测试了上面的代码,它可以工作。
编辑:我发现棒棒糖之前的设备不能使用上面的代码。有关如何添加对棒棒糖前设备的支持,请参阅此帖子:https://stackoverflow.com/a/30277424/3075340
基本上是这样做的:
Button b = (Button) findViewById(R.id.button);
ColorStateList c = ContextCompat.getColorStateList(mContext, R.color.your_custom_color;
Drawable d = b.getBackground();
if (b instanceof AppCompatButton) {
// appcompat button replaces tint of its drawable background
((AppCompatButton)b).setSupportBackgroundTintList(c);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Lollipop button replaces tint of its drawable background
// however it is not equal to d.setTintList(c)
b.setBackgroundTintList(c);
} else {
// this should only happen if
// * manually creating a Button instead of AppCompatButton
// * LayoutInflater did not translate a Button to AppCompatButton
d = DrawableCompat.wrap(d);
DrawableCompat.setTintList(d, c);
b.setBackgroundDrawable(d);
}
【讨论】:
在运行时,您知道希望按钮具有什么样式。因此,事先,在布局文件夹中的 xml 中,您可以使用所需样式的所有准备就绪按钮。因此,在布局文件夹中,您可能有一个名为:button_style_1.xml 的文件。该文件的内容可能如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/styleOneButton"
style="@style/FirstStyle" />
如果您正在使用片段,那么在 onCreateView 中您会为该按钮充气,例如:
Button firstStyleBtn = (Button) inflater.inflate(R.layout.button_style_1, container, false);
container 是与创建片段时覆盖的 onCreateView 方法关联的 ViewGroup 容器。
还需要两个这样的按钮吗?你可以这样创建它们:
Button secondFirstStyleBtn = (Button) inflater.inflate(R.layout.button_style_1, container, false);
Button thirdFirstStyleBtn = (Button) inflater.inflate(R.layout.button_style_1, container, false);
您可以自定义这些按钮:
secondFirstStyleBtn.setText("My Second");
thirdFirstStyleBtn.setText("My Third");
然后您将自定义的风格化按钮添加到您也在 onCreateView 方法中膨胀的布局容器中:
_stylizedButtonsContainer = (LinearLayout) rootView.findViewById(R.id.stylizedButtonsContainer);
_stylizedButtonsContainer.addView(firstStyleBtn);
_stylizedButtonsContainer.addView(secondFirstStyleBtn);
_stylizedButtonsContainer.addView(thirdFirstStyleBtn);
这就是您可以动态使用风格化按钮的方式。
【讨论】:
我使用持有人模式为此制作了一个帮助界面。
public interface StyleHolder<V extends View> {
void applyStyle(V view);
}
现在,对于您要实用的每种样式,只需实现接口即可,例如:
public class ButtonStyleHolder implements StyleHolder<Button> {
private final Drawable background;
private final ColorStateList textColor;
private final int textSize;
public ButtonStyleHolder(Context context) {
TypedArray ta = context.obtainStyledAttributes(R.style.button, R.styleable.ButtonStyleHolder);
Resources resources = context.getResources();
background = ta.getDrawable(ta.getIndex(R.styleable.ButtonStyleHolder_android_background));
textColor = ta.getColorStateList(ta.getIndex(R.styleable.ButtonStyleHolder_android_textColor));
textSize = ta.getDimensionPixelSize(
ta.getIndex(R.styleable.ButtonStyleHolder_android_textSize),
resources.getDimensionPixelSize(R.dimen.standard_text_size)
);
// Don't forget to recycle!
ta.recycle();
}
@Override
public void applyStyle(Button btn) {
btn.setBackground(background);
btn.setTextColor(textColor);
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
}
在attrs.xml 中声明一个样式,此示例的样式是:
<declare-styleable name="ButtonStyleHolder">
<attr name="android:background" />
<attr name="android:textSize" />
<attr name="android:textColor" />
</declare-styleable>
这是styles.xml中声明的样式:
<style name="button">
<item name="android:background">@drawable/button</item>
<item name="android:textColor">@color/light_text_color</item>
<item name="android:textSize">@dimen/standard_text_size</item>
</style>
最后是样式保持器的实现:
Button btn = new Button(context);
StyleHolder<Button> styleHolder = new ButtonStyleHolder(context);
styleHolder.applyStyle(btn);
我发现这非常有用,因为它可以轻松重用并保持代码简洁和冗长,我建议仅将其用作局部变量,以便我们在完成设置后允许垃圾收集器完成其工作所有样式。
【讨论】:
我最近遇到了同样的问题。这是我解决它的方法。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This is the special two colors background START , after this LinearLayout, you can add all view that have it for main background-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:background="#FFFFFF"
android:orientation="horizontal"
>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000FF" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F000F0" />
</LinearLayout>
<!-- This is the special two colors background END-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="This Text is centered with a special backgound,
You can add as much elements as you want as child of this RelativeLayout"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</RelativeLayout>
谢谢!
【讨论】: