开发者社区> 问答> 正文

android 读取 attr 资源:报错

在layout中:
<TextView         android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="A"

        android:textSize="?normal_font_size"

         />
在attrs.xml中
<?xml version="1.0" encoding="utf-8"?> <resources>     <attr name="normal_font_size" format="dimension" /> </resources>

在themes.xml中
  <style name="MainTheme" parent="@android :style/Theme.Black.NoTitleBar">         <item name="normal_font_size">15px</item>     </style> 
问题:在代码中如何动态读取我在主题中定义的值?

展开
收起
kun坤 2020-06-08 19:15:16 611 0
1 条回答
写回答
取消 提交回答
  • 这样读取即可~
    final Resources.Theme theme = getTheme(); TypedArray a = theme.obtainStyledAttributes(R.styleable.MainTheme);

    int size = a.getDimensionPixelSize(R.styleable.MainTheme_normal_font_size, 0);

    a.recycle();

    ###### @迷途d书童  书童兄,求指教~。###### 只要用setTheme(R.style.MainTheme)就可以了
    参考:http://www.cnblogs.com/playing/archive/2011/04/01/2002469.html ######

    引用来自“fxtxz2”的答案

    只要用setTheme(R.style.MainTheme)就可以了 参考:http://www.cnblogs.com/playing/archive/2011/04/01/2002469.html
    感谢你的回答,我的问题的在代码中动态读取,在xml我可以使用   “?+attr” 来实现动态读取,但是在代码中就不知道用什么方法了~
    2020-06-08 19:15:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载