Android自訂群組合控制項--圖片加文字,類似視頻播放軟體的列表

來源:互聯網
上載者:User

標籤:dcl   cycle   ++   creat   context   fill   總結   測試   created   

分四步來寫:
1,群組控制項的xml;
2,自訂群組合控制項的屬性;
3,自訂繼承組合布局的class類,實現帶兩參數的構造器;
4,在xml中展示群組控制項。
具體實現過程:
一、群組控制項的xml
我接觸的有兩種方式,一種是普通的Activity的xml;一種是父節點為merge的xml。我項目中用的是第一種,但個人感覺第二種好,因為第一種多了相對或者絕對布局層。
我寫的 custom_pictext.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <ImageView        android:id="@+id/custom_pic_iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@mipmap/a" />    <TextView        android:id="@+id/custom_date_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@id/custom_pic_iv"        android:layout_marginBottom="5dp"        android:layout_marginLeft="8dp"        android:text="2017" />    <TextView        android:id="@+id/custom_text_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/custom_pic_iv"        android:layout_marginLeft="4dp"        android:layout_marginTop="4dp"        android:text="題目" /></RelativeLayout>

這裡展示一個merge的例子,有時間,大家可以自己體會下。

<merge xmlns:android="http://schemas.android.com/apk/res/android">    <Button        android:id="@+id/title_bar_left"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:layout_marginLeft="5dp"        android:background="@null"        android:minHeight="45dp"        android:minWidth="45dp"        android:textSize="14sp" />    <TextView        android:id="@+id/title_bar_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:singleLine="true"        android:textSize="17sp" />    <Button        android:id="@+id/title_bar_right"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:layout_marginRight="7dp"        android:background="@null"        android:minHeight="45dp"        android:minWidth="45dp"        android:textSize="14sp" /></merge>

這兩個xml,都是寫在layout中的。
二、自訂群組合控制項的屬性
這步是我們自訂的重要部分之一,自訂群組件的私人特性全顯示在這。
首先在values中建立attrs.xml
然後定義屬性,如下代碼

<?xml version="1.0" encoding="UTF-8" ?><resources>    <declare-styleable name="CustomPicText">        <attr name="pic_backgroud" format="reference"/>        <attr name="pic_backgroud_width" format="dimension"/>        <attr name="pic_backgroud_height" format="dimension"/>        <attr name="pic_text" format="string"/>        <attr name="pic_text_color" format="color"/>        <attr name="pic_text_size" format="integer"/>        <attr name="pic_date" format="string"/>        <attr name="pic_date_color" format="color"/>        <attr name="pic_date_size" format="integer"/>    </declare-styleable></resources>

這裡有幾點需要注意的,第一:屬性名稱為name,第二:屬性單位為fromat。這單位包含的值可以查看這裡。
三、自訂繼承組合布局的class類,實現帶兩參數的構造器
我實現的CustomPicText.java

/** * Created by Hman on 2017/5/4. * 為了測試自訂群組合控制項 */public class CustomPicText extends RelativeLayout {    private ImageView customPicIv;    private TextView customDateTv;    private TextView customTextTv;    public CustomPicText(Context context, AttributeSet attrs) {        super(context, attrs);        // 載入layout        View view = LayoutInflater.from(context).inflate(R.layout.custom_pictext,this);        customPicIv = (ImageView) view.findViewById(R.id.custom_pic_iv);        customDateTv = (TextView) view.findViewById(R.id.custom_date_tv);        customTextTv = (TextView) view.findViewById(R.id.custom_text_tv);        // 載入自訂屬性配置        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CustomPicText);        // 為自訂屬性添加特性        if (typedArray != null) {            // 為圖片添加特性            int picBackgroud = typedArray.getResourceId(R.styleable.CustomPicText_pic_backgroud, 0);            float picWidth = typedArray.getDimension(R.styleable.CustomPicText_pic_backgroud_width,25);            float picHeight = typedArray.getDimension(R.styleable.CustomPicText_pic_backgroud_height,25);            customPicIv.setBackgroundResource(picBackgroud);//            customPicIv.setMinimumWidth(picWidth);            // 為標題設定屬性            String picText = typedArray.getString(R.styleable.CustomPicText_pic_text);            int picTextColor = typedArray.getColor(R.styleable.CustomPicText_pic_text_color,16);            int picTextSize = typedArray.getResourceId(R.styleable.CustomPicText_pic_date_size, 16);            customTextTv.setText(picText);            customTextTv.setTextColor(picTextColor);            customTextTv.setTextSize(picTextSize);            // 為日期設定屬性            String picDate = typedArray.getString(R.styleable.CustomPicText_pic_date);            int picDateColor = typedArray.getColor(R.styleable.CustomPicText_pic_date_color, 0);            int picDateSize = typedArray.getResourceId(R.styleable.CustomPicText_pic_date_size, 12);            customDateTv.setText(picDate);            customDateTv.setTextColor(picDateColor);            customDateTv.setTextSize(picDateSize);            typedArray.recycle();        }    }}

在這裡,我們也可以給控制項添加一些監聽器,大家自己去加上;這裡值得注意的是一個載入配置的類TypeArray
4,在xml中展示群組控制項
這個隨便寫到一個xml中去就行
代碼如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:hman="http://schemas.android.com/apk/res-auto"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <com.eastsun.widget.CustomPicText        android:id="@+id/first"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        hman:pic_backgroud="@mipmap/b"        hman:pic_date="2017/5/6"        hman:pic_date_color="@color/white"        hman:pic_text="第一張圖片"        hman:pic_text_color="@color/red"        hman:pic_text_size="18"></com.eastsun.widget.CustomPicText></LinearLayout>

這裡有一點別忘記,添加xmlns:hman=”http://schemas.android.com/apk/res-auto”
總結
程式基本上到這就結束了。
看下效果

Android自訂群組合控制項--圖片加文字,類似視頻播放軟體的列表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.