Android自定义属性时format

来源:互联网 发布:python程序调试 编辑:程序博客网 时间:2024/04/30 22:30
用到了http://blog.csdn.net/heng615975867/article/details/12834833的东西 说明一下 
</pre><pre name="code" class="java"><declare-styleable name="Panel">       <!-- Defines panel animation duration in ms. -->        <attr name="animationDuration" format="integer" />        <!-- Defines panel position on the screen. -->        <attr name="position">        <!-- Panel placed at top of the screen. -->            <enum name="top" value="0" />            <!-- Panel placed at bottom of the screen. -->            <enum name="bottom" value="1" />            <!-- Panel placed at left of the screen. -->            <enum name="left" value="2" />            <!-- Panel placed at right of the screen. -->            <enum name="right" value="3" />        </attr>        <!-- Identifier for the child that represents the panel's handle. -->        <attr name="handle" format="reference" />        <!-- Identifier for the child that represents the panel's content. -->        <attr name="content" format="reference" />        <!-- Defines if flying gesture forces linear interpolator in animation. -->        <attr name="linearFlying" format="boolean" />        <!-- Defines size relative to parent (must be in form: nn%p). -->        <attr name="weight" format="fraction" />        <!-- Defines opened handle (drawable/color). -->        <attr name="openedHandle" format="reference|color" />        <!-- Defines closed handle (drawable/color). -->        <attr name="closedHandle" format="reference|color" />  </declare-styleable>
自己的理解arrts.xml中存放的是你自定义控件的一些你自己的属性,比如<attr name="animationDuration" format="integer" />  的name为animationDuration,属性为integer,所以当你在布局文件中需要添加上这个属性是,就可以根据declare-styleable的name来引用了,我这了declare-styleable的name为Panel,所以我需要是 panel:animationDuration="1000"这样引用,给他附一个integer的值,同理其他的也是同样的意思,<attr name="weight" format="fraction" />中的fraction是分数的意思,顾名思义这里的weight需要一个百分比 的值,所以在布局文件中,如果需要,需要给他一个百分比,reference英语是参考的意思,熟悉值是一个资源ID,所以在布局文件中需要给他一个ID,这个ID不管是你自己+id还是已经存在的string id还是drawable id.具体的引用:
 <com.android.applite.view.Panel        android:id="@+id/topPanel"        android:layout_width="match_parent"        android:layout_height="wrap_content"        panel:openedHandle="@drawable/app_hub_game_handle_down"        panel:closedHandle="@drawable/app_hub_game_handle_up"        panel:animationDuration="1000"        panel:content="@+id/panelContent"        android:layout_gravity="bottom"        panel:handle="@+id/panelHandle"        panel:linearFlying="true"        panel:position="bottom" >
然后需要在构造函数中找出这些属性,因为你有的属性不一定会设置,所以需要在get的时候写入一个默认值,当你没有设置这个属性时,你自定义的控件会使用这个属性值,我对layout.xml的理解为,当你调用setContentView(R.layout.main);这句时,相当与你将这个布局里的控件都new了一边,所以在你自定义的控件的构造函数中,需要你arrts.xml中所有属性都找出,获取方法是先TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Panel);找到属性数组,然后在通过get找到不同的值 mDuration = a.getInteger(R.styleable.Panel_animationDuration, 750)防止我们在xml文件中没有定义.从而使用默认值!TypedArray.recycle() 方法,为了保持以后使用该属性一致性!
public Panel(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Panel);mDuration = a.getInteger(R.styleable.Panel_animationDuration, 750); // duration// defaults// to// 750// msmPosition = a.getInteger(R.styleable.Panel_position, BOTTOM); // position// defaults// to// BOTTOMmLinearFlying = a.getBoolean(R.styleable.Panel_linearFlying, false); // linearFlying// defaults// to// falsemWeight = a.getFraction(R.styleable.Panel_weight, 0, 1, 0.0f); // weight// defaults// to// 0.0if (mWeight < 0 || mWeight > 1) {mWeight = 0.0f;Log.w(TAG, a.getPositionDescription()+ ": weight must be > 0 and <= 1");}mOpenedHandle = a.getDrawable(R.styleable.Panel_openedHandle);mClosedHandle = a.getDrawable(R.styleable.Panel_closedHandle);RuntimeException e = null;mHandleId = a.getResourceId(R.styleable.Panel_handle, 0);if (mHandleId == 0) {e = new IllegalArgumentException(a.getPositionDescription()+ ": The handle attribute is required and must refer to a valid child.");}mContentId = a.getResourceId(R.styleable.Panel_content, 0);if (mContentId == 0) {e = new IllegalArgumentException(a.getPositionDescription()+ ": The content attribute is required and must refer to a valid child.");}a.recycle();


写面的是从一个博客里面看到的,引用一下http://blog.csdn.net/heng615975867/article/details/12834833

需要注意的两点:1,在layout.xml中,要加上xmlns:panel="http://schemas.android.com/apk/res/com.applite.android",其中panel随便写,com.applite.android是你这个工程的包名,在清单文件中.2,必须在你的自定义布局中引用,最好是在自定义控件的构造函数中a.get...()出你在layout中设置的属性,因为我一开始alt+/没有出来,在构造函数中加上后出来了

1. reference:参考某一资源ID。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="名称">
<attr format="reference" name="background" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID" />
2. color:颜色值。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="名称">
<attr format="color" name="textColor" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<TextView
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00" />
3. boolean:布尔值。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="名称">
<attr format="boolean" name="focusable" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<Button
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true" />
4. dimension:尺寸值。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="名称">
<attr format="dimension" name="layout_width" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<Button
android:layout_width="42dip"
android:layout_height="42dip" />
5. float:浮点值。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="AlphaAnimation">
<attr format="float" name="fromAlpha" />
<attr format="float" name="toAlpha" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.7" />
6. integer:整型值。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="AnimatedRotateDrawable">
<attr format="integer" name="frameDuration" />
<attr format="integer" name="framesCount" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<animated-rotate
android:frameDuration="100"
android:framesCount="12"
/>
7. string:字符串。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="MapView">
<attr format="string" name="apiKey" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
8. fraction:百分数。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="RotateDrawable">
<attr format="fraction" name="pivotX" />
<attr format="fraction" name="pivotY" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<rotate
android:pivotX="200%"
android:pivotY="300%"
/>
9. enum:枚举值。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<LinearLayout
android:orientation="vertical" >
</LinearLayout>
10. flag:位或运算。
(1)属性定义:
view plaincopyprint?
<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="0" />
<flag name="stateUnchanged" value="1" />
<flag name="stateHidden" value="2" />
<flag name="stateAlwaysHidden" value="3" />
<flag name="stateVisible" value="4" />
<flag name="stateAlwaysVisible" value="5" />
<flag name="adjustUnspecified" value="0x00" />
<flag name="adjustResize" value="0x10" />
<flag name="adjustPan" value="0x20" />
<flag name="adjustNothing" value="0x30" />
</attr>
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<activity
android:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden" >
</activity>
注意:属性定义时可以指定多种类型值:
(1)属性定义:
view plaincopyprint?
<declare-styleable name="名称">
<attr format="reference|color" name="background" />
</declare-styleable>
(2)属性使用:
view plaincopyprint?
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID|#00FF00" />

                                             
0 0
原创粉丝点击