Pla*_*ush 42 android android-layout
我通过一些按钮以编程方式为AlertDialog创建LinearLayout.
我WANT做到这一点:
<LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
style="@android:style/ButtonBar">
Run Code Online (Sandbox Code Playgroud)
但是使用这样的代码:
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");
Run Code Online (Sandbox Code Playgroud)
如何以编程方式设置按钮的样式(使用按钮栏)?
waq*_*lam 49
希望我加入派对还不算太晚:)
好吧,样式可以应用于View
初始化阶段.例如:
LinearLayout button = new LinearLayout(context, null, android.R.style.ButtonBar);
Run Code Online (Sandbox Code Playgroud)
Chr*_*the 36
这对我有用:
int buttonStyle = R.style.your_button_style;
Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle)
Run Code Online (Sandbox Code Playgroud)
Lui*_*ola 12
这是实际提供我所寻找的唯一答案:http://belencruz.com/2013/04/set-styles-programmatically-in-android/
基本上,创建一个布局XML文件,其中只包含<Button/>
具有所需样式的元素.然后,以编程方式对其进行充气,只需自定义按钮文本即可.
有点像:
<?xml version="1.0" encoding="utf-8"?>
<Button style="@style/your_custom_style_here"/>
Run Code Online (Sandbox Code Playgroud)
在代码中:
Button button = (Button)getLayoutInflater().inflate(R.layout.your_custom_button_layout_file, null);
button.setText("Your Text");
Run Code Online (Sandbox Code Playgroud)
Chr*_*acy 10
AirBnB的Paris库非常适合这种情况,允许像这样进行编程配置:
Paris.styleBuilder(textView)
.add(R.style.MyGreenTextView)
.textSizeRes(R.dimen.my_text_size_small)
.apply();
Run Code Online (Sandbox Code Playgroud)
而不是waqaslams
LinearLayout button = new LinearLayout(context, null, android.R.style.ButtonBar);
Run Code Online (Sandbox Code Playgroud)
(也就是说,通过评论判断,API可靠)我也从这里读了ridoys的答案[ 编程的Android按钮样式 ]这是
transferBtn.setBackgroundResource(R.layout.buttonstyle);
Run Code Online (Sandbox Code Playgroud)
(揭露上述答案,因为根据您使用的API版本,其中一个也可能对您有用),
但这对我有用(记住从"布局"到"可绘制"的变化):
Button b = new Button(this);
b.setBackgroundResource(R.drawable.button_custom_green);
Run Code Online (Sandbox Code Playgroud)
(从[ 如何在视图中以编程方式设置样式属性回答]
归档时间: |
|
查看次数: |
59872 次 |
最近记录: |