最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年05月20日 16:55:27   作者:TheMrNice  
這篇文章主要為大家分享了Android自定義控件之組合控件學(xué)習筆記,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下

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

這里寫圖片描述

大家看到這個覺得這不是很簡單的嗎,這不就是寫個布局文件就搞定嘛,沒錯,確實直接上布局就行,不過,我只是用這個簡單的例子來講一下自定義組合控件的用法。

首先看看,這一行行的條目看起來都長得差不多,只是圖片和文字不一樣,沒錯,就是看中這一點,我們可以把一個條目做成一個組合控件,做為一個整體,這樣不管你有幾個條目,就寫幾個組合控件就行了。

步驟:

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.新建一個類MyView繼承RelativeLayout,將自定義的布局文件加載進來并且獲取自定義的屬性,然后取得自定義屬性字段的值,最后將相應(yīng)的值設(shè)置在相應(yīng)的組件上

/**
 * 自定義組合控件(包括一個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) {
  //把自定義的布局加載進來
  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文件中添加自定義組合控件

注:記得加上命名空間
有幾個條目就加幾個控件
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>

這里寫圖片描述

注:

這里寫圖片描述

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

相關(guān)文章

  • Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例

    Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例

    這篇文章主要介紹了Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能,結(jié)合具體實例形式分析了Android使用json格式數(shù)據(jù)在服務(wù)器與客戶端傳遞實現(xiàn)數(shù)據(jù)庫查詢功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android 消息機制以及handler的內(nèi)存泄露

    Android 消息機制以及handler的內(nèi)存泄露

    這篇文章主要介紹了Android 消息機制以及handler的內(nèi)存泄露的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android實現(xiàn)長按圓環(huán)動畫View效果的思路代碼

    Android實現(xiàn)長按圓環(huán)動畫View效果的思路代碼

    這篇文章主要介紹了Android實現(xiàn)長按圓環(huán)動畫View效果,本文給大家分享實現(xiàn)思路,通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Android ConstraintLayout約束布局使用實例介紹

    Android ConstraintLayout約束布局使用實例介紹

    ConstraintLayout是Google在Google I/O 2016大會上發(fā)布的一種新的布局容器(ViewGroup),它支持以靈活的方式來放置子控件和調(diào)整子控件的大小,下面這篇文章主要給大家介紹了關(guān)于Android中ConstraintLayout約束布局詳細解析的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Android 網(wǎng)絡(luò)請求框架解析之okhttp與okio

    Android 網(wǎng)絡(luò)請求框架解析之okhttp與okio

    HTTP是現(xiàn)代應(yīng)用常用的一種交換數(shù)據(jù)和媒體的網(wǎng)絡(luò)方式,高效地使用HTTP能讓資源加載更快,節(jié)省帶寬,OkHttp是一個高效的HTTP客戶端,下面這篇文章主要給大家介紹了關(guān)于OkHttp如何用于安卓網(wǎng)絡(luò)請求,需要的朋友可以參考下
    2021-10-10
  • Android Retrofit框架的使用

    Android Retrofit框架的使用

    這篇文章主要介紹了Android Retrofit框架的使用,幫助大家更好的理解和學(xué)習使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android之EditText控制禁止輸入空格和回車

    Android之EditText控制禁止輸入空格和回車

    本文主要介紹了Android中使用EditText控制禁止輸入空格和回車的實現(xiàn)代碼。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android App中使用Pull解析XML格式數(shù)據(jù)的使用示例

    Android App中使用Pull解析XML格式數(shù)據(jù)的使用示例

    這篇文章主要介紹了Android App中使用Pull解析XML格式數(shù)據(jù)的使用示例,Pull是Android中自帶的XML解析器,Java里也是一樣用:D需要的朋友可以參考下
    2016-04-04
  • Android圖片加載案例分享

    Android圖片加載案例分享

    這篇文章主要為大家分享了Android圖片加載的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android 通過jni返回Mat數(shù)據(jù)類型方法

    Android 通過jni返回Mat數(shù)據(jù)類型方法

    今天小編就為大家分享一篇Android 通過jni返回Mat數(shù)據(jù)類型方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評論

鄂伦春自治旗| 沂水县| 辉南县| 定远县| 内江市| 南澳县| 盈江县| 浦江县| 灵台县| 砀山县| 岑巩县| 平果县| 象州县| 宁城县| 贡觉县| 固原市| 张家港市| 林芝县| 徐闻县| 西城区| 麟游县| 重庆市| 达孜县| 德阳市| 沐川县| 上蔡县| 宜都市| 确山县| 大连市| 晴隆县| 西青区| 花莲县| 沙雅县| 鹤壁市| 怀安县| 南郑县| 建阳市| 柳林县| 永顺县| 河北省| 泰和县|