美文网首页Android Other
19.Android中的 ?attr/ 是怎么回事

19.Android中的 ?attr/ 是怎么回事

作者: Jsonzhang | 来源:发表于2017-05-08 16:21 被阅读512次

问题概述

从github上下载的项目中我们经常会看到xml布局中有这样一句android:background="?attr/elephantTheme",有时也会是图片类型,我们点进去,会发现这个值是在values/attr下面的文件里,如下:

<resources>
   <attr name="elephantTheme" format="color" />
</resources>

然后我就不知道了这个属性是怎么赋值的,然后项目全局搜索elephantTheme,会发现在valuse/style文件中会有这个值,如下:

<style name="WhiteTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="elephantTheme">@color/theme_white_theme</item>
</style>
<style name="BlueTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="elephantTheme">@color/theme_blue_theme</item>
</style>
<style name="GrayTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="elephantTheme">@color/theme_gray_theme</item>
</style>

这是凭感觉这个属性值,是在这里被赋值的,但疑问三个值,他是根据什么取到想要的属性值,猜测肯定是哪里设置了style,在xml布局中没有找到设置的style,那就去activity中找,也没有,后来才baseactivity中果然找到了设置theme的代码。如下:

 private void initTheme() {
        mThemeUtil = ThemeUtil.getInstance(this);
        String theme = mThemeUtil.getTheme();
        switch (theme) {
            case Constants.Theme.Blue:
                setTheme(R.style.BlueTheme);
                break;

            case Constants.Theme.White:
                setTheme(R.style.WhiteTheme);
                break;

            case Constants.Theme.Gray:
                setTheme(R.style.GrayTheme);
                break;

            default:
                setTheme(R.style.BlueTheme);
                break;
        }
    }

综上所述

我们知道了大概流程,以及这个属性是怎么赋值的,但是我不知道具体原理,以及不确定。所以自己需要简单的验证下。
就是在自己的activity中给一个button通过同样的方式设置背景。
首先
values/attrs.xml中创建自己的属性(button的背景):

<resources>
    <attr  name="btn_bg" format="color"></attr>
</resources>

其次
valuse/style.xml中新建自定义样式

 <style name="BlueTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="btn_bg">#0000ff</item>
  </style>
  <style name="RedTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="btn_bg">#ff0000</item>
  </style>

再次
在activity的布局中,给button布局设置背景:

<Button
    android:background="?attr/btn_bg"   //这里
    android:id="@+id/btn_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="登录" />

最后
在acitivity中设置theme:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.RedTheme);   //这里
        setContentView(R.layout.activity_login);
 }

运行后果然是,你设置哪种theme,button的backgroud会相应的获取对应的值。

结束……

相关文章

  • 19.Android中的 ?attr/ 是怎么回事

    问题概述 从github上下载的项目中我们经常会看到xml布局中有这样一句android:background="...

  • 前端学习笔记

    jQuery中attr()和prop()的使用和区别 一、attr()使用 $(selector).attr():...

  • Jquery中的`attr`和`prop`的区别

    Jquery中的attr和prop的区别 测试唤醒: Google attr和prop的作用和区别   attr和...

  • 理解values—attr文件

    attr全称attribute,意思是属性,性质的意思(个人理解),Android中的attr文件用于描述自定义v...

  • jQuery内容整理——属性操作

    属性——attr与prop 1.attr 获取匹配元素集合中第一个元素的属性的值$('p').attr('titl...

  • jQuery笔记2 常用DOM方法记录

    查找节点 获取页面中的某个指定节点 attr方法 attr方法是用来获取节点的某个属性的,类似于js的getAtt...

  • 自定义View 入门

    attrs values 文件夹中定义attr属性文件 attr的format取值类型( 属性定义时可以指定多种类...

  • jquery-操作DOM

    使用attr()方法控制元素的属性 attr()方法的作用是设置或者返回元素的属性,其中attr(属性名)格式是获...

  • JQ笔记

    1:attr()方法的作用是设置或者返回元素的属性,其中attr(属性名)格式是获取元素属性名的值,attr(属性...

  • jQuery中的attr()方法

    js购物车

网友评论

    本文标题:19.Android中的 ?attr/ 是怎么回事

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