android 自訂群組合控制項

來源:互聯網
上載者:User

android 自訂群組合控制項

自訂控制項是一些android程式員感覺很難攻破的痛點,起碼對我來說是這樣的,但是我們可以在網上找一些好的部落格關於自訂控制項好好拿過來學習研究下,多練,多寫點也能找到感覺,把一些原理弄懂,今天就講下自訂群組合控制項,這個特別適合在標題列或者設定介面,看下面圖:


就非常適合使用群組控制項了,現在寫一個玩玩:


activity_main.xml

               

SettingView.java
public class SettingView extends RelativeLayout {private TextView title;private TextView content;public SettingView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView(context);}public SettingView(Context context, AttributeSet attrs) {super(context, attrs);initView(context);/** * 擷取一個樣式的屬性集 * 把布局檔案xml中的擷取到的樣式集合attrs跟自己自訂的樣式集合建立一個映射關係 */TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingView);String title = a.getString(0);String content = a.getString(1);//設定到介面上setTitle(title);setContent(content);a.recycle();}public SettingView(Context context) {super(context);initView(context);}private View initView(Context context){View view = View.inflate(context, R.layout.setting_view, this);title = (TextView) view.findViewById(R.id.title);content = (TextView) view.findViewById(R.id.content);return view;}public void setTitle(String tilte){title.setText(tilte);}public void setContent(String strContent){content.setText(strContent);}public void setTextSize(int size){content.setTextSize(size);title.setTextSize(size);}}

setting_view.xml



android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#22000000"
>


android:layout_width="fill_parent"
android:layout_height="wrap_content"

>

android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
/>

android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_below="@id/title"
/>

android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>



android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#000000"
android:layout_below="@id/cb"
/>

我們發現在activity_main.xml中有二個屬性

hy:title = "標題1"
hy:content = "內容1"

這個時候xml檔案會報錯

error: Error parsing XML: unbound prefix 說沒有hy這樣的首碼,因為我們寫android:什麼屬性 都是因為有xmlns:android ="http://schemas.android.com/apk/res/android"這樣一個命名空間在,那我們也自訂一個命名空間,仿照上面的寫就是了xmlns:hy ="http://schemas.android.com/apk/res/com.example.customcircle"//res後面的是你得包名
現在xml檔案有報錯了:Multiple annotations found at this line: - error: No resource identifier found for attribute 'content' in package 'com.example.customcircle'說在我們的包下沒有找到conent的定義
而 hy:title = "標題" hy:content = "內容"是我自訂的屬性如果要使用自訂的屬性,就要把這些屬性定義出來,而為什麼系統的android:屬性名稱---就可以呢?那是因為android sdk已經聲明好了,在找到我們使用的sdkD:\java\tools\adt-bundle-windows-x86_64-20130917\sdk\platforms\android-10\data\res\values在這個目錄下有一個attrs.xml檔案,我們就找一個平時大家都會用到的















這就是TextView中的屬性,所以我們也要模仿他建立一個attrs檔案在value目錄下
declare-styleable 聲明的意思 name表示 聲明的控制項名稱我們的 "SettingView"> 當儲存的時候就會在你R.java檔案中產生了一個內部類 public static final class attr {//屬性集 }然後聲明屬性"title" format="string">"content" format="string" >name表示屬性的名稱 format表示屬性的類型
然後我們儲存代碼 就沒錯誤提示瞭然後再看R.java檔案 public static final class attr { /**

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form"@[ package :]type : name") ortheme attribute (in the form"?[ package :][type :] name")containing a value of this type. */ public static final int content=0x7f010001; /**

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form"@[ package :]type : name") ortheme attribute (in the form"?[ package :][type :] name")containing a value of this type. */ public static final int title=0x7f010000; }這個時候我們運行下我們的項目,發現這自訂的屬性並沒有起作用,那意思就是說我們要用代碼的方式讓它顯示出來
AttributeSet類表示屬性的集合 在我們定義的屬性比如: android:id= "@+id/sv1" android:layout_width="fill_parent" android:layout_height="wrap_content" hy:title = "標題" hy:content = "內容"相當於把這些屬性封裝成了一個類,這真是我們java強大之處,物件導向編程思想
然在建構函式中做這個操作
public SettingView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); /** * 擷取一個樣式的屬性集 * 把布局檔案 xml中的擷取到的樣式集合 attrs跟自己自訂的樣式集合建立一個映射關係 */ TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingView ); String title = a.getString(0); String content = a.getString(1); //設定到介面上 setTitle(title); setContent(content); a.recycle(); }這樣就算成功了

運行效果:


總結下自訂群組合控制項的步驟:

1. 寫一個類 繼承 ViewGroup LinearLayout RelativeLayout


2. 如果在布局檔案裡面定義view對象 使用這個view對象的兩個參數的構造方法.


3. 定義這個自訂控制項裡面要顯示的內容
View.inflate(context, R.layout.ui_setting_view, this);


4. 添加自訂控制項的屬性.
定義自訂屬性的命名空間


5. 聲明自訂的屬性





觀察R檔案 產生 attr內部類 產生styleable 數組 所有的attrs


6. 在xml布局檔案裡面配置 自訂的屬性.


7. 在自訂view對象的構造方法裡面 擷取AttributeSet
跟我們自訂的屬性建立映射關係


8. 在自訂群組合控制項裡面 設定 布局檔案的資料, 擴充點擊事件.


9. 在布局檔案使用自訂的view對象.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.