Ing*_*yng 1 android android-fragments android-studio
这是我的风格:
<style name="buttonQuestionStyle" parent="@style/Widget.AppCompat.Button.Colored">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">16sp</item>
<item name="android:padding">25dp</item>
<item name="android:layout_margin">10dp</item>
<item name="android:background">@color/questionButton</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
Button btn = new Button(getActivity());
btn.setText(ojb.getText());
if (Build.VERSION.SDK_INT < 23) {
btn.setTextAppearance(getActivity(), R.style.buttonQuestionStyle);
} else {
btn.setTextAppearance(R.style.buttonQuestionStyle);
}
Run Code Online (Sandbox Code Playgroud)
在应用程序中:
以编程方式显示如下按钮:

通过布局它起作用了。出现如下:

这是我在XML布局中的代码:
<Button
android:text="Question"
style="@style/buttonQuestionStyle" />
Run Code Online (Sandbox Code Playgroud)
所以...我不知道为什么会发生,以及如何解决。
您可以ContextThemeWrapper为按钮传递in构造函数,并为3参数使用构造函数Button(context, attributeset, defStyle)。
ContextThemeWrapper wrapper = new ContextThemeWrapper(this,R.style.buttonQuestionStyle);
Button btn = new Button(wrapper, null, 0); // note this constructor
btn.setText("some text");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1310 次 |
| 最近记录: |