美文网首页Android开发
更改Android AppCompatRadioButton控件

更改Android AppCompatRadioButton控件

作者: 我是一座离岛 | 来源:发表于2017-01-02 18:18 被阅读883次

准确来讲是更改它的主题颜色,默认启用的是colorPrimary的值

1.在你的build.gradle 文件中引入最新的appcompat依赖包

  dependencies { 
        compile 'com.android.support:appcompat-v7:X.X.X' // X.X.X 为版本号
     }

2.使你的activity继承android.support.v7.app.AppCompatActivity

public class MainActivity extends AppCompatActivity { 
  ...
}

3.在styles.xml文件中定义你想要的样式,格式如下

<style name="MyRadioButton" parent="Theme.AppCompat.Light"> 
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/green</item>
</style>

4.在使用AppCompatRadioButton的地方使用属性android:theme应用自定义的样式

<android.support.v7.widget.AppCompatRadioButton 
    android:id="@+id/r3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:theme="@style/MyRadioButton" />

效果:

参考:

http://www.materialdoc.com/radio-button/
http://stackoverflow.com/questions/26562609/change-widgets-color-appcompat-21

相关文章

网友评论

    本文标题:更改Android AppCompatRadioButton控件

    本文链接:https://www.haomeiwen.com/subject/wunwvttx.html