zoukankan      html  css  js  c++  java
  • xmlns:android作用以及自定义布局属性

    要定制Android layout 中的 attributes关键是要明白android中命名空间定义如:

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


    以RingtonePreference为例::

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/sound_settings"
        android:key="sound_settings"
        xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">

    <com.android.settings.DefaultRingtonePreference
        android:key="ringtone"
        android:title="@string/ringtone_title"
        android:summary="@string/ringtone_summary"
        android:dialogTitle="@string/ringtone_title"
        android:persistent="false"
        android:ringtoneType="ringtone" />


    在代码中::

    public RingtonePreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.RingtonePreference, defStyle, 0);
        mRingtoneType = a.getInt(com.android.internal.R.styleable.RingtonePreference_ringtoneType,
                RingtoneManager.TYPE_RINGTONE);
        mShowDefault = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showDefault,
                true);

     

        mShowSilent = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showSilent,
                true);
        a.recycle();
    }

    这里注意了ringtoneType的命名空间使用的是android, 而其容器中声明了两个命名空间android, settings
    ::

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

    何为命名空间呢?里面定义了各个类所用的属性的定义。 android这个命名空间就对应了/frameworks/base/core/res/res/values/attrs.xml文件中
    定义的属性值;而settings这个命名空间就是Settings应用的res/values/attrs.xml或settings_attrs.xml文件中的属性.

    如果我们查看frameworks/base/core/res/res/values/attrs.xml里面有对DefaultRingtonePreference的父类RingtonePreference的名字空间的定义:

    ::

    <!-- Base attributes available to RingtonePreference. -->
    <declare-styleable name="RingtonePreference">
        <!-- Which ringtone type(s) to show in the picker. -->
        <attr name="ringtoneType">
            <!-- Ringtones. -->
            <flag name="ringtone" value="1" />
            <!-- Notification sounds. -->
            <flag name="notification" value="2" />
            <!-- Alarm sounds. -->
            <flag name="alarm" value="4" />
            <!-- All available ringtone sounds. -->
            <flag name="all" value="7" />
        </attr>
        <!-- Whether to show an item for a default sound. -->
        <attr name="showDefault" format="boolean" />
        <!-- Whether to show an item for 'Silent'. -->
        <attr name="showSilent" format="boolean" />
    </declare-styleable>

    上例中declear-styleable中的属性name对应的类名,attr则是类中的属性.

     
     
     
     
     
  • 相关阅读:
    Codeforces 1291 Round #616 (Div. 2) B
    总结
    刷新DNS解析缓存+追踪+域名解析命令
    数学--数论--Hdu 5793 A Boring Question (打表+逆元)
    Lucene.net(4.8.0) 学习问题记录六:Lucene 的索引系统和搜索过程分析
    LeetCode 117 Populating Next Right Pointers in Each Node II
    LeetCode 116 Populating Next Right Pointers in Each Node
    test test
    LeetCode 115 Distinct Subsequences
    LeetCode 114. Flatten Binary Tree to Linked List
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4504456.html
Copyright © 2011-2022 走看看