以编程方式更改按钮的主题

时间:2016-03-18 11:50:30

标签: android android-button android-theme android-styles

我可以使用xml中的android:theme属性将主题设置为按钮。是否有任何等效的程序代码?

<Button
        android:id="@+id/btnPayment"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="Add Payment"
        android:theme="@style/ButtonPeter" // theme..
        />

style.xml

<style name="ButtonPeter" parent="Widget.AppCompat.Button">
        <item name="colorButtonNormal">@color/fbutton_color_peter_river</item>
        <item name="android:textColor">@android:color/white</item>

    </style>

我可以使用此类btnPayment.setTheme(R.style.ButtonPeter)动态设置主题吗?

我搜索了很多,但所有帖子都与集style相关或创建动态button并对其应用样式但我不想这样做。我想将theme设置为button

如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

我认为您对styletheme感到困惑。从document开始,theme表示整个活动,style用于查看。

  

样式是一组属性,用于指定视图或窗口的外观和格式。

     

主题是应用于整个活动或应用程序的样式,而不是单个视图。

对于此代码:

android:theme="@style/ButtonPeter" // theme is used but only valid style attributes for button will be applied

希望得到这个帮助。

答案 1 :(得分:0)

在我提到 here 时,使用TextViewCompat.setTextAppearance应该有效: - )

相关问题