android 资源相关基础

来源:互联网 发布:软件开发工程师月薪 编辑:程序博客网 时间:2024/04/28 08:43

1.attrs.xml

属性定义:

<declare-styleablename="Theme">

<attrname="buttonStyle" format="reference" />

<attrname="windowNoTitle" format="boolean" />

<attrname="alertDialogStyle" format="reference" />

</declare-styleable>

<declare-styleablename="AlertDialog">

        <attr name="fullDark"format="reference|color" />

        <attr name="topDark"format="reference|color" />

        <attr name="centerDark"format="reference|color" />

        <attr name="bottomDark"format="reference|color" />

        <attr name="fullBright"format="reference|color" />

        <attr name="topBright"format="reference|color" />

        <attr name="centerBright"format="reference|color" />

        <attr name="bottomBright"format="reference|color" />

        <attr name="bottomMedium"format="reference|color" />

        <attr name="centerMedium"format="reference|color" />

</declare-styleable>

<attrname="textSize" format="dimension" />

<declare-styleablename="TextAppearance">

        <attr name="textColor"/>

        <attr name="textSize"/>

</declare-styleable>

特点:未曾定义的,指定名称跟类型;曾定义的,只指定名称。

      可以在declare-styleable标签内,也可以独立定义

      每个属性都会生成对应的id,而AlertDialog会生成一个int数组,包含了其所有属性的值,另外还会生成AlertDialog_fullDark.

Xml引用:

android:src

style="?android:attr/textAppearanceLarge"

  延伸(参考CellLayout):xmlns:android="http://schemas.android.com/apk/res/android"

       xmlns:launcher="http://schemas.android.com/apk/res/launcher"

        launcher:cellWidth

已知Theme.xml中定义:

<itemname="textAppearanceLarge">@android:style/TextAppearance.Large</item>

问题:style="?android:attr/textAppearanceLarge"与style="android:style/TextAppearance.Large"一样吗?

代码引用:

TypedArray a = mContext.obtainStyledAttributes(

                null,com.android.internal.R.styleable.AlertDialog,com.android.internal.R.attr.alertDialogStyle, 0);

int fullDark = a.getResourceId(

               R.styleable.AlertDialog_fullDark, R.drawable.popup_full_dark);

说明:style="?android:attr/textAppearanceLarge"表示的是使用系统当前选用的主题中属性textAppearanceLarge所对应的样式;style="android:style/TextAppearance.Large"直接指定具体的样式。前者会根据系统选用的默认样式显示不同的效果,后者只有一个效果;只有当textAppearanceLarge所对应的样式为TextAppearance.Large,二者效果才会相同。

2.styles.xml 与 themes.xml

<stylename="AlertDialog">

        <itemname="fullDark">@android:drawable/popup_full_dark</item>

        <itemname="topDark">@android:drawable/popup_top_dark</item>

        <itemname="centerDark">@android:drawable/popup_center_dark</item>

        <itemname="bottomDark">@android:drawable/popup_bottom_dark</item>

        <itemname="fullBright">@android:drawable/popup_full_bright</item>

        <itemname="topBright">@android:drawable/popup_top_bright</item>

        <itemname="centerBright">@android:drawable/popup_center_bright</item>

        <itemname="bottomBright">@android:drawable/popup_bottom_bright</item>

        <itemname="bottomMedium">@android:drawable/popup_bottom_medium</item>

        <itemname="centerMedium">@android:drawable/popup_center_medium</item>

</style>

<stylename="Theme">

<item name="buttonStyle">@android:style/Widget.Button</item>

<itemname="windowNoTitle">false</item>

<itemname="alertDialogStyle">@android:style/AlertDialog</item>

</style>

共同点:

属性定义与引用类似。

差异:

Theme是针对窗体级别的,改变窗体样式;
Style是针对窗体元素级别的,改变指定控件或者Layout的样式。

例如窗体级别引用:

<stylename="ftxActivity"parent="android:style/Theme">

        <itemname="android:windowNoTitle">true</item>

</style>

<activity

            android:name=".activity.LoadingActivity"

            android:label="@string/app_name"

            android:theme="@style/ftxActivity">

控件级样式引用:

<stylename="titleAppearance"parent="android:TextAppearance">

        <itemname="android:textColor">#ffffffff</item>

        <itemname="android:textSize">18sp</item>

        <itemname="android:textStyle">bold</item> 

    </style>

<TextView

            android:id="@+id/title"

            style="@style/titleAppearance"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"/>

3.ids.xml

定义:<item type="id" name="background" />

思考:<item type="id" name="mogoo_background"value="@android:id/background"/>

mogoo_background的值 与 background的值一样吗?

引用:

@id/ 与 @+id

如果在@后面使用“+”,表示当修改完某个布局文件并保存后,系统会自动在R.java文件中
生成相应的int类型变量。变量名就是“/”后面的值。如果在R.java中已经存在同名的变量,
就不再生成新的变量,而该组件会使用这个已存在的变量的值。

4.状态选择器(颜色、图片)

颜色xml定义:

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

    <itemandroid:state_pressed="true"android:color="#ee6b14"/>

    <itemandroid:state_selected="true"android:color="#ee6b14"/>

    <itemandroid:color="#ffffffff"/>

</selector>

xml引用:

android:textColor="@color/news_review_lebel_color"

代码引用:

Resources.getColorStateListid)

图片xml定义:

<selector

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

    <itemandroid:state_pressed="true"android:drawable="@drawable/arrow_pressed"/>

    <itemandroid:drawable="@drawable/arrow"/>

</selector>

xml引用:

android:background="@drawable/back_btn_bg"

代码引用:

Resources.getDrawableid)

5.多分辨率的适配

为了适配不同的分辨率,可以为相关分辨率配置不同的资源图片,甚至可以使用不同的布局,设置不同的dimen,此处只作部分举例说明,实际中应用的灵活性更高,方案更多。

1)图片资源

drawable-hdpi ---------density 240的机子对应图片

drawable-xhdpi---------- density 320的机子对应图片

drawable-nodpi-----------不区分density的图片,不拉伸

drawable-960x540 ----------分辨率960x540对应图片

2)布局资源

layout-sw550dp------------------屏幕宽超550dp后使用的布局

layout-h550dp------------------屏幕高超550dp后使用的布局

layout-land------------------屏幕横屏使用的布局

layout-port------------------屏幕横屏使用的布局

layout-hdpi------------------density 240的机子对应的布局

layout-960x540 ------------------分辨率960x540对应布局

3)dimen资源

dimens.xml中定义:

<dimenname="gif_vedio_name_size">8sp</dimen>

代码引用:

Resources.getDimensionPixelSize

xml引用:

android:textSize="@dimen/gif_vedio_name_size"

不同分辨率需要设置不同的gif_vedio_name_size,可以通过以下方式:

values-sw600dp-------------屏幕宽超550dp后使用

values-h550dp -------------屏幕高超550dp后使用