【问题标题】:Android - How to programmatically set the button style in a Linearlayout?Android - 如何以编程方式设置线性布局中的按钮样式?
【发布时间】:2012-07-10 10:55:30
【问题描述】:

我正在以编程方式为带有一些按钮的 AlertDialog 创建 LinearLayout。

想要这样做:

<LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
style="@android:style/ButtonBar">

但是使用这样的代码:

LinearLayout buttons = new LinearLayout(parentContext);
buttons.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams buttonsParams =
    new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT);
topLayout.addView(buttons, buttonsParams);
buttons.setLayoutParams(buttonsParams);
Button btnAdd = new Button(context);
btnAdd.setText("Add");

如何以编程方式设置按钮的样式(使用按钮栏)?

【问题讨论】:

标签: android android-layout


【解决方案1】:

在我将样式传递给Button 构造函数时,根本没有加载样式。 这似乎是因为我使用的是材料设计视图。这解决了我的问题:

val themeWrapper = ContextThemeWrapper(
      context,
      R.style.AppTheme_ButtonPrimary // my button style
)

val button = MaterialButton(themeWrapper)

请注意,MaterialButton 是创建的,而不是 Button

我在How to specify a style for a view programmatically in Android找到了这个解决方案。

【讨论】:

    【解决方案2】:

    AirBnB 的 Paris 库非常适合此功能,允许像这样进行编程配置:

    Paris.styleBuilder(textView)
            .add(R.style.MyGreenTextView)
            .textSizeRes(R.dimen.my_text_size_small)
            .apply();
    

    【讨论】:

      【解决方案3】:

      这对我有用:

      int buttonStyle = R.style.your_button_style;
      Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle)
      

      【讨论】:

        【解决方案4】:

        这是唯一真正实现了我正在寻找的答案:http://belencruz.com/2013/04/set-styles-programmatically-in-android/

        基本上,创建一个布局 XML 文件,该文件只包含一个具有所需样式的 &lt;Button/&gt; 元素。然后,以编程方式对其进行充气并自定义按钮文本。

        类似的东西:

        <?xml version="1.0" encoding="utf-8"?>
        <Button style="@style/your_custom_style_here"/>
        

        在代码中:

        Button button = (Button)getLayoutInflater().inflate(R.layout.your_custom_button_layout_file, null);
        button.setText("Your Text");
        

        【讨论】:

        • 这不会改变我的风格
        • 谢谢!显然是唯一可行的解​​决方案。使用ContextThemeWrapper 或在 Button 构造函数中指定样式不会应用所有属性。
        【解决方案5】:

        而不是 waqaslams

        LinearLayout button = new LinearLayout(context, null, android.R.style.ButtonBar);
        

        (也就是说,从 cmets 判断,API 可靠)我也从这里阅读了 ridoys 的答案 [Android Button Styling Programatically] 是

        transferBtn.setBackgroundResource(R.layout.buttonstyle);
        

        (公开上述答案,因为其中一个也可能对您有用,具体取决于您使用的 API 版本),

        但这对我有用(注意从“布局”到“可绘制”的变化)

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

        (来自 [How to programmatically setting style attribute in a view 的回答)

        【讨论】:

          【解决方案6】:

          希望我加入派对还不算太晚:)

          嗯,样式可以在初始化阶段应用于View。例如:

          LinearLayout button = new LinearLayout(context, null, android.R.style.ButtonBar);
          

          【讨论】:

          • 这仅适用于 API 级别 11,您不知道如何在旧 API 中执行此操作吗?
          • @Oliv 我相信旧 API 不可能。但是,作为替代方案,我建议您将 LinearLayout 定义为具有您想要的所有样式的 XML,然后简单地将 XML 膨胀到代码中的对象。
          • 如果我将 Button 更改为 LinearLayout,我会在 Button 类上失去其他方法,例如 setText()
          • 对我不起作用。结果,按钮根本没有样式,看起来像纯文本
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-15
          • 1970-01-01
          • 2014-09-20
          • 2011-06-06
          • 1970-01-01
          • 2013-05-09
          相关资源
          最近更新 更多