Android - 如何在 Linearlayout 中以编程方式设置按钮样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11422220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:21:46  来源:igfitidea点击:

Android - How to programmatically set the button style in a Linearlayout?

androidandroid-layout

提问by PlanetaryFortressRush

I am programmatically creating a LinearLayout for an AlertDialog with some buttons.

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

I WANTto do this:

WANT做到这一点:

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

But with code like this:

但是用这样的代码:

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

How can I set the style of the buttons (use a button bar) programmitically?

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

回答by waqaslam

Hope I'm not too late to join the party :)

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

Well, Stylescan be applied to a Viewat initialization phase. For example:

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

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

回答by Chris Lamothe

This worked for me:

这对我有用:

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

回答by Luis Artola

This is the only answer that actually delivered what I was looking for: http://belencruz.com/2013/04/set-styles-programmatically-in-android/

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

Essentially, create a layout XML file that simply has a <Button/>element with the style you want. Then, inflate it programmatically and just customize the button text.

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

Something along the lines of:

类似的东西:

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

In code:

在代码中:

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

回答by Chris Lacy

The Parislibrary from AirBnB is perfect for this, allowing for programmatic configuration like so:

AirBnB的Paris库非常适合这种情况,允许像这样进行编程配置:

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

回答by Uяo?Koт

Instead of waqaslams

代替瓦卡斯拉姆

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

(which is, judging by the comments, API dependable) I have also read ridoys answer from here [ Android Button Styling Programatically] which is

(也就是说,从评论来看,API 可靠)我还从这里阅读了ridoys 的答案 [ Android Button Styling Programatically],它是

transferBtn.setBackgroundResource(R.layout.buttonstyle);

(exposing the above answers because one of these may also work for you, depending of API version you use),

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

But this is what worked for me(mind the change from "layout" to "drawable"):

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

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

(answer from [How to programmatically setting style attribute in a view)

(来自[如何在视图中以编程方式设置样式属性的答案)

回答by user9440008

In my case style was not loaded at all when I pass them into Buttonconstructor. It seems that was because I was using the material design view. Here what solved my case:

在我的情况下,当我将它们传递给Button构造函数时,根本没有加载样式。似乎是因为我使用的是材料设计视图。这里是什么解决了我的案子:

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

val button = MaterialButton(themeWrapper)

Please note that MaterialButtonwas created instead of Button.

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

I found this solution at: How to specify a style for a view programmatically in Android.

我在以下位置找到了这个解决方案:如何在 Android 中以编程方式指定视图的样式