我正在使用以下代码从XML中获取视图:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
我想为按钮设置"style",由于我要使用几种不同的样式来区分每个按钮,请问在Java中如何实现这一点。
首先,你不需要使用布局膨胀器来创建一个简单的按钮。你只需要使用:
button = new Button(context);
如果您想为按钮设置样式,有两个选择:最简单的方法是只需在代码中指定所有元素,就像其他答案所建议的那样:
button.setTextColor(Color.RED);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
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);
此方法无法用于更改所有属性;例如,要更改填充(padding),您需要使用ContextThemeWrapper
。但对于文本颜色、大小等,您可以使用setTextAppearance
。
Button
可能处于的每个状态(如聚焦、选定、按下、禁用等)定义不同的可绘制项。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文档...http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29 - Christopher Orr您可以这样设置样式属性:
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"
/>
是的,你可以在按钮中使用例如(for example)
Button b = new Button(this);
b.setBackgroundResource(R.drawable.selector_test);
如果您正在使用支持库,您可以简单地使用以下方法
TextViewCompat.setTextAppearance(textView, R.style.AppTheme_TextStyle_ButtonDefault_Whatever);
对于TextView和Button,其余视图也有类似的类 :-)
<?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);
<resources>
<color name="silver">#C0C0C0</color>
</resources>
如果有人想要 Material 的答案,请参考这篇 Stack Overflow 帖子:Android 中使用 Material Design 和 AppCompat 给按钮上色
我使用了这篇答案的结合体,将我的按钮的默认文本颜色设置为白色:https://dev59.com/R18d5IYBdhLWcg3wt0EQ#32238489
然后,在此答案https://dev59.com/R18d5IYBdhLWcg3wt0EQ#34355919中找到通过编程方式设置背景颜色的方法。 代码如下:
ViewCompat.setBackgroundTintList(your_colored_button,
ContextCompat.getColorStateList(getContext(),R.color.your_custom_color));
Button
或者AppCompat按钮作为your_colored_button
- 我已经测试过这两种按钮类型,它们都可以使用上述代码。请注意:在Android 5.0以下的设备上,上述代码可能无法正常工作。您可以参考此文内的内容来添加对这些设备的支持:https://dev59.com/DYrda4cB1Zd3GeqPSu3F#30277424
具体操作步骤如下:
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 version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/styleOneButton"
style="@style/FirstStyle" />
Button firstStyleBtn = (Button) inflater.inflate(R.layout.button_style_1, container, false);
在创建fragment时,如果要重写onCreateView方法,其中container参数是与之关联的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");
_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);
我发现这非常有帮助,因为它可以轻松地重复使用并保持代码的简洁和冗长,我建议仅将其用作本地变量,以便我们在设置所有样式后允许垃圾收集器完成其工作。