【问题标题】:How to programmatically set style attribute in a view如何以编程方式在视图中设置样式属性
【发布时间】:2011-01-02 05:47:58
【问题描述】:

我使用以下代码从 XML 中获取视图:

Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);

我想为按钮设置一个“样式”,我该如何在 java 中做到这一点,因为我想为我将使用的每个按钮使用多种样式。

【问题讨论】:

    标签: android styles


    【解决方案1】:

    首先,您不需要使用布局充气器来创建简单的 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 - 这是我一直在寻找的东西。
    • 这非常有效。一直在寻找这个解决方案。
    • 该版本的 setTextAppearance 已被弃用。你可以只使用 button.setTextAppearance(R.style.MyTextStyle);
    • ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle); button = new Button(newContext); 假设在按钮创建时选择样式。如果创建按钮后需要更改样式怎么办?
    【解决方案2】:

    通常您不能以编程方式更改样式;您可以使用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

    【讨论】:

    • 我的问题是我从描述我的按钮信息的网络服务加载数据。按钮需要根据它们所属的类别具有不同的样式。这就是我想动态设置样式的原因。
    • 好吧,您不能更改 Android style 属性,但您可以像使用任何其他视图一样以编程方式设置 Button 的背景,如果这足够的话。此外,作为Button 继承自TextView,您可以更改文本属性。只需查看这些项目的 API 文档...developer.android.com/reference/android/view/…
    • 我只是在考虑这样做,就在检查我是否有新答案之前。非常感谢。
    • 是否无法为定义文本大小、边距等的按钮创建样式,然后以编程方式将其应用于按钮。如果我有六个想要具有相同样式的按钮,肯定可以说?
    • 我们可以扩充哪个属性?我们可以增加边距,填充吗?
    【解决方案3】:

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

    Button b = new Button(this);
    b.setBackgroundResource(R.drawable.selector_test);
    

    【讨论】:

    • 这将设置背景,但样式可以指定的不仅仅是背景。许多视图(包括按钮)似乎都有一个以样式 ID 作为参数的构造函数。但是,我无法让它为我工作 - 按钮显示为没有边框或任何东西的文本。我可能会做的是创建一个只有一个按钮的布局(指定样式),扩展该布局,然后设置按钮文本等等。
    【解决方案4】:

    你可以像这样做样式属性:

    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"
    
        />
    

    【讨论】:

    • 应该创建一个考虑到按钮的好答案。很高兴看到如何为现有按钮执行此操作。
    • 查看 cesards 的答案。
    【解决方案5】:

    如果您使用的是 Support 库,您可以简单地使用

    TextViewCompat.setTextAppearance(textView, R.style.AppTheme_TextStyle_ButtonDefault_Whatever);
    

    用于文本视图和按钮。其余视图也有类似的类:-)

    【讨论】:

    • 你需要使用哪个 R 包来获得这种风格?
    【解决方案6】:

    根据您想要更改的样式属性,您可以使用 Paris 库:

    Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
    Paris.style(view).apply(R.style.YourStyle);
    

    支持许多属性,如背景、填充、textSize、textColor 等。

    免责声明:我创作了这个库。

    【讨论】:

      【解决方案7】:

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

      【讨论】:

      • @Pacerier:这个问题根本没有这么说。样式是 XML 还是 java 是模棱两可的。它只询问如何以编程方式设置样式。
      【解决方案8】:

      对于任何寻找材料答案的人,请参阅此 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);
      }
      

      【讨论】:

        【解决方案9】:

        在运行时,您知道希望按钮具有什么样式。因此,事先,在布局文件夹中的 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);
        

        这就是您可以动态使用风格化按钮的方式。

        【讨论】:

          【解决方案10】:

          我使用持有人模式为此制作了一个帮助界面。

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

          我发现这非常有用,因为它可以轻松重用并保持代码简洁和冗长,我建议仅将其用作局部变量,以便我们在完成设置后允许垃圾收集器完成其工作所有样式。

          【讨论】:

            【解决方案11】:

            我最近遇到了同样的问题。这是我解决它的方法。

            <?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>
            
            • 我使用了带有 android:weightSum="2" 的 LinearLayout
            • 我给了两个子元素 android:layout_weight="1" (我给了每个父空间的 50%(宽度和高度))
            • 最后,我为这两个子元素赋予了不同的背景颜色以产生最终效果。

            谢谢!

            【讨论】:

              猜你喜欢
              • 2011-03-15
              • 1970-01-01
              • 1970-01-01
              • 2022-12-23
              • 2021-06-22
              • 2023-01-03
              • 1970-01-01
              相关资源
              最近更新 更多