欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

這里寫圖片描述

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

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

步驟:

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,將自定義的布局文件加載進(jìn)來并且獲取自定義的屬性,然后取得自定義屬性字段的值,最后將相應(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) {
  //把自定義的布局加載進(jìn)來
  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é)習(xí)Android自定義控件有所幫助。

相關(guān)文章

  • android panellistview 圓角實現(xiàn)代碼

    android panellistview 圓角實現(xiàn)代碼

    android panellistview 圓角是每一個android開發(fā)者都具備的一項,對于新手朋友來說可能有點難度,接下來將詳細(xì)介紹,需要了解的朋友可以參考下
    2012-12-12
  • Android實現(xiàn)的仿淘寶購物車demo示例

    Android實現(xiàn)的仿淘寶購物車demo示例

    這篇文章主要介紹了Android實現(xiàn)的仿淘寶購物車demo示例,結(jié)合實例形式分析了Android購物車的功能、布局及邏輯實現(xiàn)技巧,需要的朋友可以參考下
    2016-07-07
  • ProgressBar、ProgessDialog-用法(詳解)

    ProgressBar、ProgessDialog-用法(詳解)

    下面小編就為大家?guī)硪黄狿rogressBar、ProgessDialog-用法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Android中查看USB連接的外接設(shè)備信息的代碼實例

    Android中查看USB連接的外接設(shè)備信息的代碼實例

    這篇文章主要介紹了Android中查看USB連接的外接設(shè)備信息的代碼實例,需要的朋友可以參考下
    2014-04-04
  • Android實現(xiàn)手勢滑動識別功能

    Android實現(xiàn)手勢滑動識別功能

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)手勢滑動識別功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • android從系統(tǒng)圖庫中取圖片的實例代碼

    android從系統(tǒng)圖庫中取圖片的實例代碼

    這篇文章主要介紹了android從系統(tǒng)圖庫中取圖片的方法,涉及Android讀取及選擇圖片等相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Android ImageView實現(xiàn)圖片裁剪和顯示功能

    Android ImageView實現(xiàn)圖片裁剪和顯示功能

    這篇文章主要介紹了Android ImageView實現(xiàn)圖片裁剪和顯示功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • Android截屏方案實現(xiàn)原理解析

    Android截屏方案實現(xiàn)原理解析

    這篇文章給大家介紹了Android截屏方案實現(xiàn)原理解析,包括普通截屏,Scrollview截屏,Scrollview截屏,RecyclerView截屏等,具體截屏實現(xiàn)原理,大家參考下本文
    2017-12-12
  • Android接入阿里云熱修復(fù)介紹

    Android接入阿里云熱修復(fù)介紹

    大家好,本篇文章主要講的是Android接入阿里云熱修復(fù)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android自定義仿ios加載彈窗

    Android自定義仿ios加載彈窗

    這篇文章主要為大家詳細(xì)介紹了Android自定義仿ios加載彈窗,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05

最新評論