日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Andriod: 在xml布局中使用自定义属性

發布時間:2024/4/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Andriod: 在xml布局中使用自定义属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天在看android froyo的launcher2 源碼的時候,在launcher.xml中看到有這么一段代碼:

<com.android.launcher2.DragLayerxmlns:android="http://schemas.android.com/apk/res/android"xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"android:id="@+id/drag_layer"android:layout_width="match_parent"android:layout_height="match_parent"><include layout="@layout/all_apps" /><!-- The workspace contains 3 screens of cells --><com.android.launcher2.Workspaceandroid:id="@+id/workspace"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="horizontal"android:fadeScrollbars="true"launcher:defaultScreen="2">

注意到其中的兩處:

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

launcher:defaultScreen="2"

可以看出在這個布局文件中,使用了自定義屬性。

?

以前沒遇到過,既然這里碰到了,就順便學習下,下面就寫個簡單的示例,權當學習筆記,便于以后查閱。

1. 定義一些自定義屬性

建立一個屬性xml文件: values/attrs.xml, 內容如下:

<?xml version="1.0" encoding="utf-8" ?> <resources><!-- the relation between the icon and text. --><attr name="relation"><enum name="icon_left" value="0" /><enum name="icon_right" value="1" /><enum name="icon_above" value="2" /><enum name="icon_below" value="3" /></attr><skip /><declare-styleable name="IconText"><attr name="relation" /><attr name="icon" format="reference" /> <attr name="text" format="string" /><attr name="text_size" format="dimension" /><attr name="text_color" format="integer" /><attr name="space" format="dimension" /></declare-styleable></resources>

解釋如下:

屬性relation有4種可選值:icon_left, icon_right, icon_above,icon_below.

屬性icon的可選值為引用: 例如:"@/drawbable/icon".

屬性text的可選值為string, 例如: "Hello world", 也可是string的引用"@string/hello".

屬性text_size的可選值為尺寸大小,例如:20sp、18dip、20px等.

屬性text_color的可選值為整數,例如:"0xfffffff", 也可以是color的引用"@color/white".

?

2. 定義一個能夠處理這些屬性值的view或者layout類

package com.braincol.viewattrs;import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView;public class IconTextView extends LinearLayout {private final static String TAG = "IconTextView";private final int ICON_LEFT = 0;private final int ICON_RIGHT = 1;private final int ICON_ABOVE = 2;private final int ICON_BELOW = 3;private TextView mTextView;private ImageView mImageView;private int mRelation = ICON_LEFT;private String mText = "";private int mIconId;private float mTextSize;private int mSpace;public IconTextView(Context context, AttributeSet attrs){super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconText);mRelation = a.getInt(R.styleable.IconText_relation, ICON_LEFT);Log.d(TAG,"mRelation: "+mRelation);mText = a.getString(R.styleable.IconText_text);Log.d(TAG,"mText: "+mText);mTextSize = a.getDimensionPixelSize(R.styleable.IconText_text_size, 12);Log.d(TAG,"mTextSize: "+mTextSize);mSpace = a.getDimensionPixelSize(R.styleable.IconText_space, 5);Log.d(TAG,"mSpace: "+mSpace);mIconId = a.getResourceId(R.styleable.IconText_icon, R.drawable.icon);Log.d(TAG,"mIconId: "+mIconId);a.recycle();mTextView = new TextView(context);mTextView.setText(mText);mTextView.setTextSize(mTextSize);mImageView = new ImageView(context);mImageView.setImageResource(mIconId); int left = 0;int top = 0;int right = 0;int bottom = 0;int orientation = HORIZONTAL;int textViewIndex = 0;switch(mRelation){case ICON_ABOVE:orientation = VERTICAL;bottom = mSpace;textViewIndex = 1;break;case ICON_BELOW:orientation = VERTICAL;top = mSpace;break;case ICON_LEFT:right = mSpace;textViewIndex = 1;break;case ICON_RIGHT:left = mSpace;break;}this.setOrientation(orientation);this.addView(mImageView);mImageView.setPadding(left, top, right, bottom);this.addView(mTextView, textViewIndex);} }

可以看出這個LinearLayout 子類IconTextView中只有兩個元素,ImageView 和mTextView,通過從xml配置文件中讀取屬性值來決定icon和text的內容、相對位置及其它屬性。

?

3. 在layout布局文件中使用這個自定布局及其屬性

layout/main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <com.braincol.viewattrs.IconTextViewandroid:id="@+id/icontext_01"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:icontext="http://schemas.android.com/apk/res/com.braincol.viewattrs"android:layout_width="wrap_content"android:layout_height="wrap_content"icontext:relation="icon_left"icontext:icon="@drawable/hi"icontext:text="hi,how are you!"icontext:text_size="12sp"/></LinearLayout>

可以看到我們在這個布局文件中加入了一個新的命名空間:

xmlns:icontext="http://schemas.android.com/apk/res/com.braincol.viewattrs"???

并使用我們自定義的那些屬性:

icontext:relation="icon_left"
icontext:icon="@drawable/hi"
icontext:text="hi, how are you !"
icontext:text_size="30sp"

4. 在Activity中使用該布局

package com.braincol.viewattrs;import android.app.Activity; import android.os.Bundle;public class ViewAttrs extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);} }

運行效果:

轉載于:https://www.cnblogs.com/hibraincol/archive/2011/10/27/2227149.html

總結

以上是生活随笔為你收集整理的Andriod: 在xml布局中使用自定义属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。