我从XML获得了一个视图,其代码如下:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
Run Code Online (Sandbox Code Playgroud)
我想为按钮设置一个"样式"我怎么能在java中这样做,因为我想使用几个样式我将使用的每个按钮.
Chr*_*Orr 51
通常,您无法以编程方式更改样式; 您可以使用主题或样式设置 XML布局中的屏幕外观或部分布局或单个按钮.但是,可以通过编程方式应用主题.
还有一个东西StateListDrawable
可以让你为你Button
可以进入的每个状态定义不同的drawable ,无论是聚焦,选择,按下,禁用等等.
例如,要使按钮在按下时更改颜色,您可以定义一个名为res/drawable/my_button.xml
directory 的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>
Run Code Online (Sandbox Code Playgroud)
然后,您可以Button
通过设置属性将此选择器应用于a android:background="@drawable/my_button"
.
bee*_*tra 43
首先,您不需要使用布局inflater来创建简单的Button.你可以使用:
button = new Button(context);
Run Code Online (Sandbox Code Playgroud)
如果你想设置按钮的样式,你有两个选择:最简单的就是在代码中指定所有元素,就像许多其他答案所示:
button.setTextColor(Color.RED);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Run Code Online (Sandbox Code Playgroud)
另一种选择是在XML中定义样式,并将其应用于按钮.在一般情况下,您可以使用a ContextThemeWrapper
:
ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
button = new Button(newContext);
Run Code Online (Sandbox Code Playgroud)
要更改TextView(或其子类,如Button)上与文本相关的属性,有一种特殊方法:
button.setTextAppearance(context, R.style.MyTextStyle);
Run Code Online (Sandbox Code Playgroud)
最后一个不能用于更改所有属性; 例如,要更改填充,您需要使用a ContextThemeWrapper
.但是对于文本颜色,大小等,您可以使用setTextAppearance
.
Day*_*man 14
是的,您可以在按钮中使用
Button b = new Button(this);
b.setBackgroundResource(R.drawable.selector_test);
Run Code Online (Sandbox Code Playgroud)
ces*_*rds 10
如果您使用支持库,则只需使用
TextViewCompat.setTextAppearance(textView, R.style.AppTheme_TextStyle_ButtonDefault_Whatever);
Run Code Online (Sandbox Code Playgroud)
用于TextViews和Buttons。其余的视图也有类似的类:-)
Kri*_*lsh 10
你可以这样做样式属性:
Button myButton = new Button(this, null,android.R.attr.buttonBarButtonStyle);
Run Code Online (Sandbox Code Playgroud)
取代:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
style="?android:attr/buttonBarButtonStyle"
/>
Run Code Online (Sandbox Code Playgroud)
根据您想要更改的样式属性,您可以使用 Paris 库:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
Paris.style(view).apply(R.style.YourStyle);
Run Code Online (Sandbox Code Playgroud)
支持许多属性,如背景、填充、文本大小、文本颜色等。
免责声明:我编写了该库。
对于任何寻找材质答案的人来说,请看这篇SO帖子:Android中使用Material Design和AppCompat的着色按钮
我使用了这个答案的组合将按钮的默认文本颜色设置为白色为我的按钮:https: //stackoverflow.com/a/32238489/3075340
然后这个回答/sf/answers/2404914361/以编程方式设置背景颜色.代码是:
ViewCompat.setBackgroundTintList(your_colored_button,
ContextCompat.getColorStateList(getContext(),R.color.your_custom_color));
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
@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>
Run Code Online (Sandbox Code Playgroud)
然后在Java中
((Button) findViewById(R.id.my_button)).setEnabled(false);
((Button) findViewById(R.id.my_button)).setBackgroundResource(R.drawable.button_disabled);
Run Code Online (Sandbox Code Playgroud)
这会将按钮的属性设置为禁用,并将颜色设置为银色.
[颜色在color.xml中定义为:
<resources>
<color name="silver">#C0C0C0</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
196556 次 |
最近记录: |