如何以编程方式在视图中设置样式属性

Lin*_*nt_ 99 android styles

我从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.xmldirectory 的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".

  • 是否无法为定义文本大小,边距等的按钮创建样式,然后以编程方式将其应用于按钮.当然可以说如果我有六个按钮想要具有相同的风格? (5认同)
  • 我的问题是我从描述我的按钮信息的web服务加载数据.按钮需要根据它们所属的类别具有不同的样式.这就是我想动态设置风格的原因. (4认同)
  • 那么你不能改变Android`style`属性,但你可以用任何其他视图以编程方式设置`Button`的背景,如果这样就足够了.此外,由于`Button`继承自`TextView`,您可以更改文本属性.只需查看这些项目的API文档... http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29 (3认同)

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.

  • `ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle); Button = new Button(newContext);` 假设选择按钮创建时的样式。如果我在创建按钮后需要更改样式怎么办? (3认同)
  • 它仅适用于某些属性 (2认同)
  • 太棒了。ContextThemeWrapper - 是我一直在寻找的东西。 (2认同)

Day*_*man 14

是的,您可以在按钮中使用

Button b = new Button(this);
b.setBackgroundResource(R.drawable.selector_test);
Run Code Online (Sandbox Code Playgroud)

  • 这将设置背景,但样式可以指定的不仅仅是背景.很多视图(包括Button)似乎都有一个构造函数,它将样式ID作为参数.但是,我有问题让它为我工作 - 按钮显示为没有边框或任何东西的文本.我可能会做的是创建一个只有一个按钮的布局(指定一个样式),膨胀该布局,然后设置按钮文本和诸如此类的东西. (5认同)

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)

  • 考虑按钮应该创建好的答案.很高兴看到如何为现有按钮执行此操作. (5认同)

Nat*_*ael 7

根据您想要更改的样式属性,您可以使用 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)

支持许多属性,如背景、填充、文本大小、文本颜色等。

免责声明:我编写了该库。


Mic*_*cro 6

对于任何寻找材质答案的人来说,请看这篇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_buttonButton如果你愿意,可以只是一个普通的或一个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)


bin*_*iam 5

@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)

  • @Pacerier:问题根本没有说明.风格是XML还是java都不明确.它只询问如何以编程方式设置样式. (4认同)