編輯:關於 Android 程式設計
來聊聊自定義組合控件,相信大家多少都接觸過。話不多說,直接開幹(哈~哈~):

看到這個,你可能覺得不就是寫個布局檔搞定嘛——沒錯,確實直接上布局就行,我只是用這個簡單的例子來講解自定義組合控件的用法。
仔細看,這一行行條目長得都差不多,只是圖片和文字不同。正因為看準了這一點,我們可以把一個條目做成一個組合控件,作為一個整體,這樣不管你有多少個條目,只要寫幾個組合控件就行了。
步驟:
1. 先建立組合控件的布局
myView.xml
<RelativeLayout xmlns:android="https://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:text /> <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,加載自定義布局、取得自定義屬性,再把值設置到相應控件上
/** * 自定義組合控件(包括一個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); //把值設置到相應組件上 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="https://schemas.android.com/apk/res/android xmlns:briup="https://schemas.android.com/apk/res/com.example.test" xmlns:tools="https://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>

註:

做到以上步驟就可以了,希望本文所述對大家學習 Android 自定義控件有所幫助。
Android 沉浸式狀態欄實作:Toolbar 與自訂 TopBar 兩種方案
沉浸式狀態欄可讓狀態欄與 App 內容更自然地融合。使用 Toolbar 時,可先關閉 ActionBar,再為 Toolbar 設定 fitsSystemWindows
用貝塞爾曲線復刻 QQ 拖拽清除效果
純粹出於好奇心寫的一個學習 Demo,先上效果: 那兩條帶弧度、會隨手指拉伸變形的邊界,本質上就是貝塞爾曲線。 Android 的 Path 已經內建了繪製貝塞爾曲
為 Android RecyclerView 加上「上拉加載更多」
上一篇已經介紹了如何為 RecyclerView 加上 FootView,在此基礎上要實作分頁加載,其實已經非常簡單了。 上一篇:為 RecyclerView 添加 Fo
如何預置 Android 手機 APK:四種情境完整教學
預置 APK 依需求可分為以下四種情境: 1. 如何將「帶源碼」的 APK 預置進系統? 2. 如何將「無源碼」的 APK 預置進系統? 3. 如何預置 APK 使得使用