国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android自定義控件之組合控件學(xué)習(xí)筆記分享

2019-12-12 06:22:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

我們來(lái)講一下自定義組合控件,相信大家也接觸過(guò)自定義組合控件吧,話不多說(shuō),直接干(哈~哈~):

這里寫圖片描述

大家看到這個(gè)覺(jué)得這不是很簡(jiǎn)單的嗎,這不就是寫個(gè)布局文件就搞定嘛,沒(méi)錯(cuò),確實(shí)直接上布局就行,不過(guò),我只是用這個(gè)簡(jiǎn)單的例子來(lái)講一下自定義組合控件的用法。

首先看看,這一行行的條目看起來(lái)都長(zhǎng)得差不多,只是圖片和文字不一樣,沒(méi)錯(cuò),就是看中這一點(diǎn),我們可以把一個(gè)條目做成一個(gè)組合控件,做為一個(gè)整體,這樣不管你有幾個(gè)條目,就寫幾個(gè)組合控件就行了。

步驟:

1.先建立組合控件的布局

myView.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp" > <ImageView  android:id="@+id/icon_Iv"  android:layout_width="35dp"  android:layout_height="35dp"  android:layout_centerVertical="true"  android:layout_marginLeft="30dp"  android:src="@drawable/phone_qiyi_explore_friends" /> <TextView  android:id="@+id/tv"  android:layout_width="wrap_content"  android:layout_height="match_parent"  android:layout_marginLeft="80dp"  android:gravity="center"  android:text="朋友圈"  android:textSize="15sp"  android:textStyle="bold" /> <ImageView  android:layout_width="wrap_content"  android:layout_height="match_parent"  android:layout_alignParentRight="true"  android:layout_marginRight="20dp"  android:src="@drawable/phone_my_inc_arrow" /> <View  android:layout_width="match_parent"  android:layout_height="0.5dp"  android:layout_alignParentBottom="true"  android:layout_marginLeft="5dp"  android:layout_marginRight="5dp"  android:background="#000" /></RelativeLayout>

這里寫圖片描述

2.自定義屬性(圖片資源和文本)

在values/目錄下新建attrs.xml文件

attrs.xml:

<?xml version="1.0" encoding="utf-8"?><resources> <!-- 自定義屬性:src和text --> <declare-styleable name="myView_attrs">  <attr name="src" format="reference"></attr>  <attr name="text" format="string"></attr> </declare-styleable></resources>

這里寫圖片描述

3.新建一個(gè)類MyView繼承RelativeLayout,將自定義的布局文件加載進(jìn)來(lái)并且獲取自定義的屬性,然后取得自定義屬性字段的值,最后將相應(yīng)的值設(shè)置在相應(yīng)的組件上

/** * 自定義組合控件(包括一個(gè)ImageView和TextView) * @author Administrator * */public class MyView extends RelativeLayout{ private TextView tv; private ImageView icon_Iv; public MyView(Context context) {  this(context,null); } public MyView(Context context, AttributeSet attrs) {  super(context, attrs);  initView(context);  //拿到自定義的屬性  TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myView_attrs);  //獲取自定義屬性的值  String text = ta.getString(R.styleable.myView_attrs_text);  Drawable drawable = ta.getDrawable(R.styleable.myView_attrs_src);  //把值設(shè)置到相應(yīng)組件上  icon_Iv.setImageDrawable(drawable);  tv.setText(text); } private void initView(Context context) {  //把自定義的布局加載進(jìn)來(lái)  View.inflate(context,R.layout.myview,this);  //找到布局中的組件  icon_Iv = (ImageView) this.findViewById(R.id.icon_Iv);  tv = (TextView) this.findViewById(R.id.tv); }}

4.在main.xml文件中添加自定義組合控件

注:記得加上命名空間
有幾個(gè)條目就加幾個(gè)控件
main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android xmlns:briup="http://schemas.android.com/apk/res/com.example.test" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.test.MainActivity" > <com.example.test.MyView  android:id="@+id/myView"  briup:src="@drawable/phone_qiyi_explore_friends"  briup:text="朋友圈"   android:layout_width="wrap_content"  android:layout_height="wrap_content"/> <com.example.test.MyView  android:id="@+id/myView1"  briup:src="@drawable/phone_qiyi_gusslike_icon"  briup:text="啪啪奇"   android:layout_width="wrap_content"  android:layout_height="wrap_content"/> <com.example.test.MyView  android:id="@+id/myView2"  briup:text="消息"   briup:src="@drawable/phone_qiyi_message_icon"  android:layout_width="wrap_content"  android:layout_height="wrap_content"/></LinearLayout>

這里寫圖片描述

注:

這里寫圖片描述

做到以上步驟就可以了,希望本文所述對(duì)大家學(xué)習(xí)Android自定義控件有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 静宁县| 福安市| 临洮县| 资源县| 天气| 玉龙| 中西区| 内江市| 和平县| 衡水市| 洱源县| 苗栗县| 阜宁县| 攀枝花市| 梨树县| 阿克| 沐川县| 江阴市| 阿城市| 瑞安市| 赤峰市| 嘉禾县| 巴林左旗| 临城县| 衡山县| 新宾| 揭西县| 长汀县| 启东市| 治县。| 南江县| 通海县| 麻阳| 科技| 新邵县| 乐东| 柳河县| 南阳市| 民乐县| 新乐市| 城口县|