Android layout文件中 '?' 的作用

在Android layout文件中,属性引用资源一般使用@,例如

android:textColor="@color/white"

但在一些系统文件中我们也可以看到有这样的写法

android:textColor="?android:color/textColor"

我们知道@是引用已经定义好的资源,如@color/white、@android:color/white,那 ‘?’ 呢?下面是文档中的解释

Referencing style attributes

A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, “use the style that is defined by this attribute, in the current theme.”

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional. For instance:

?[<package_name>:][<resource_type>/]<resource_name>

For example, here’s how you can reference an attribute to set the text color to match the “primary” text color of the system theme:

<EditText id="text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="?android:textColorSecondary"
    android:text="@string/hello_world" />

也就是说 ‘?’ 用来引用主题中的资源值,以显示当前主题的风格类型。

一般设置主题有几个方法,

  • 在AndroidManifest中,application标签里,这样会将主题应用到application下面所有的activity中
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        ...
</application>
  • 在AndroidManifest.xml中,activity标签里,这样只给该activity设置主题
<activity
    android:name=".MainActivity"
    android:theme="@style/AppTheme"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
  • 在Activity中代码设置,需要在setContent()方法前调用
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppTheme);
    setContentView(R.layout.activity_main);
}

例子:

主题style/AppTheme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

主题style/AppTheme.Another

<style name="AppTheme.Another" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">#ffff8800</item>
</style>

可以看到主题将所有view的background都设置为#ffff8800。原因是android:background是View的一个通用属性,如果View自身没有再去设定这个值的话,就会使用当前主题下的值。

如果我们只想改变某一个view的背景颜色怎么办?这里就可以使用?这个符号。首先在attrs.xml中定义一个标签

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="customBackground" format="color|reference"></attr>
</resources>

在主题中设置这个标签的值

<style name="AppTheme.Another" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="customBackground">#ffff8800</item>
</style>

在布局中引用这个主题值

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                ...
                >

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="?attr/customBackground"
        android:textSize="30sp"/>

</RelativeLayout>

结果如下

如果我们再定义一些其他主题,如:

<style name="AppTheme.Third" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="customBackground">#ff0099cc</item>
</style>

那么只需要设置相应的主题就可以改变view的属性值。

还可以再主题里设置更多的标签,这样就可以完成像夜间主题这种背景、字体颜色等一系列属性同时改变。

Android layout文件中 '?' 的作用

时间: 2024-08-09 05:40:12

Android layout文件中 '?' 的作用的相关文章

【转】关于Android资源文件中出现百分号的问题

关于Android资源文件中出现百分号的问题 分类: Android JAVA2014-08-01 16:53 1345人阅读 评论(0) 收藏 举报 ANDROID格式化资源文件 目录(?)[+] 问题编辑strings.xml的时候 在行 [html] view plaincopy <string name="myurl">http://code.dd.com/rr?q=%rr.55</string> 或者 [html] view plaincopy <

Android资源文件中各种XML的作用与解释

众所周知,XML是一种可扩展标记语言,它被用来传输和存储数据.在Android中也会随处可见XML文件,包括一个android项目不可缺少的AndroidManifest.xml清单文件,res资源文件目录下的anim/drawable/layout/menu/values中等,目录截图如下.其中清单文件中内容最多最复杂,完全可以在其他文章中再来讲解,所以本文主要讲解res目录下的XML的作用与内容. 一.anim目录 anim目录下的xml主要是用于android中的动画,包括Frame an

Android布局文件中xml里的xmlns:的作用

一些新手经常看到布局文件中都有xmlns:android="http://schemas.android.com/apk/res/android"或者 xmlns:tools=http://schemas.android.com/tools 却不知道是什么意思. 其实它是告诉Android开发工具你准备使用Android命名空间里的一些通用属性.在所有Android XML设计文件中最外层的标记必须使用这个树形. 它可以提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为

如何在android style文件中使用自定义属性

前几天我在项目中遇到了这样一个问题:我为项目编写了一个自定义控件,这个控件会被大量复用,所以我准备在style.xml文件中定义一个style来减少重复xml布局内容的编写,但是里面有一个自定义的控件属性,问题出现在这里,虽然自定义属性在layout布局xml中可以使用正常,但是却无法在style中定义,本来这个控件是大量服用的,style也是为了复用减少xml内容写的,我可以把自定义属性内容直接写死在自定义控件中,但是考虑到项目未来可能出现的变数,我还是准备将这个自定义属性写到style中,这

Android manifest文件中的标签详细介绍

概要 每一个Android应用都应该包含一个manifest文件,即AndroidManifest.xml.它包含了程序运行的一些必备信息,比如: --为Java应用程序指定一个独一无二的名字. --描述程序所包括的成分,如activities, services, broadcast receivers和content providers等内容. --定义哪一个成分是主要的.比如主线程等. --声明程序正常运行所需要的权限.比如,读写SD卡等. --声明该程序的API Level,低于该API

android 布局文件中xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;

http://blog.163.com/benben_long/blog/static/199458243201411394624170/ xmlns:android="http://schemas.android.com/apk/res/android的作用是 这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件.或者语法判断器什么的 这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上面你写错了,

在Android.mk文件中输出打印消息

http://www.xuebuyuan.com/1947880.html 在进行Android NDK的开发当中有时想看看Android.mk文件当中某个变量的值,可以再Android.mk文件当中用warnin语句实现该功能假如有个Android.mk文件的内容如下: [plain] viewplaincopyprint? LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE    := hello-jni LOCAL

在Android.mk文件中输出打印消息 (转载)

转自:http://blog.csdn.net/xiaibiancheng/article/details/8479694 在进行Android NDK的开发当中有时想看看Android.mk文件当中某个变量的值,可以再Android.mk文件当中用warnin语句实现该功能假如有个Android.mk文件的内容如下: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FI

android layout文件优化

性能优化1--UI优化 1.使用系统为我们提供了几个抽象的标签①include:重用include中layout属性指定一个外部布局文件,通过该方式则不需要把这个布局文件在该代码中重复的写一遍了. 若include指定了其他的android:layout_*的这种属性,则layou_width和Layout_height必须存在.否则其他的无法生效 ②viewstub:按需加载viewstub标签和include标签很相似,都是使用layout属性来加载一个布局.不同之处是include标签用来