如何从 Java 代码中设置布局和组件的样式?我想做的事情如下:
button.setStyle(R.style.MyStyle);
最佳答案
您无法以编程方式在 View 上应用样式。尽管您可以像下面这样进行运行时膨胀。
创建“template.xml”布局文件。其中具有带样式的 TextView 布局。您可以创建多个来支持不同的风格。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Template"
style="@style/first_style" />
在 Activity 中,您可以通过以下代码来扩展此布局。
TextView txtView = (TextView)getLayoutInflater().inflate(R.layout.template, null);
关于java - 以编程方式设置 Android 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448638/