自定义控件例如LinearLayout 的三种方法
生活随笔
收集整理的這篇文章主要介紹了
自定义控件例如LinearLayout 的三种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近通過看別人代碼和網上搜索,發現現在自定義LinearLayout的方式有三種。
第一種是在擴展的LinearLayout構造函數中使用Inflater加載一個布局,并從中提取出相關的UI組件進行封裝,形成一個獨立的控件。在使用該控件時,由于它所有的子元素都是在運行時通過代碼動態創建的,所以該控件只能以一個獨立控件的形式在Layout文件中聲明,例如:
public class CustomLayout extends LinearLayout{
? ?? ? public??CustomLayout(Context context){
? ?? ?? ?? ?? ???LayoutInflater mInflater = LayoutInflater.from(context);
? ?? ?? ?? ?? ? View myView = mInflater.inflate(R.layout.receive, null);
? ?? ?? ?? ?? ? addView(myView);
? ?? ? }
}
< LinearLayout
??xmlns:android="http://schemas.android.com/apk/res/android"
??android:layout_width="wrap_content"
??android:layout_height="wrap_content"
??androidundefinedrientation="vertical" >
??<LinearLayout?
? ?? ?? ? android:layout_width="wrap_content"
? ?? ?? ? android:layout_height="wrap_content"
? ?? ?? ? androidundefinedrientation="horizontal">
? ?? ?? ? <TextView
? ?? ?? ?? ?? ?? ? android:layout_width="wrap_content"
? ?? ?? ?? ?? ?? ?android:layout_height="wrap_content" />
? ?? ?? ?<Button
? ?? ?? ?? ?? ???android:layout_width="wrap_content"
? ?? ?? ?? ?? ?? ?android:layout_height="wrap_content"?
? ?? ?? ?? ?? ?? ?android:id="@+id/button" />
??</LinearLayout>
??
? ?? ?? ???<TextView
? ?? ?? ?? ?? ?? ? android:layout_width="wrap_content"
? ?? ?? ?? ?? ?? ?android:layout_height="wrap_content" />? ?? ???
? ?? ?? ?? ?? ?? ??
< /LinearLayout>
第二種方式是:在自定義控件中聲明它的所有子元素,然后在Layout文件中像使用LinearLayout一樣去進行布局,不過切記:自定義控件Code中聲明的UI元素必須與Layout文件中聲明的元素保持一致,否則,在代碼中無法找到對應的控件;最后,在自定義控件的onInflateFinish中通過findViewById將layout中聲明的控件跟代碼中聲明的控件匹配起來,例如有某個自定義控件:public class CustomLayout extends LinearLayout{
? ? ListView??mListView; //代碼中聲明的控件
? ? XXXX; //針對UI控件封裝的操作
}
Layout文件中使用該布局進行聲明:
<com.XX.CustomLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? androidundefinedrientation="vertical" >
? ?? ???<ListView android:layout_width="wrap_content"? ?
? ?? ?? ? android:layout_height="fill_parent"? ?
? ?? ?? ? android:id="@+id/ListView01"??
? ?? ?? ? />
? ?? ???<.........>
< /com.XX.CustomLayout>
最后在代碼中進行匹配:
protected void onFinishInflate()? ?
? ? {? ?? ?? ?? ?? ??
? ?? ???mListView=(ListView)findViewById(R.id.ListView01);? ??
? ?? ???mListView.setAdapter(mAdapter);
? ?? ???mListView.setSelector(new ColorDrawable(Color.TRANSPARENT));
? ?? ???mListView.setBackgroundColor(0xF3EEE5);
? ?? ???mListView.setCacheColorHint(0);
? ?? ???mListView.setDivider(null);
? ?? ?? ?? ?
? ? }
第三種方式是:這個自定義VIEW中的任何控件都不是通過XML文件來定義的,而是在JAVA代碼中通過動態生成的,然后再addView()加入到你自定義的View中,
如:
private class SpeechView extends LinearLayout {??
? ?? ???private TextView mTitle;??
? ?? ???private TextView mDialogue;??
? ?? ???public SpeechView(Context context, String title, String words) {??
? ?? ?? ?? ?super(context);??
? ?? ?? ?? ?this.setOrientation(VERTICAL);??
? ?? ?? ?? ? // Here we build the child views in code. They could also have??
? ?? ?? ?? ?// been specified in an XML file.
? ?? ?? ?? ?mTitle = new TextView(context);
? ?? ?? ?? ?mTitle.setText(title);??
? ?? ?? ?? ?addView(mTitle, new LinearLayout.LayoutParams(??
? ?? ?? ?? ?? ?? ? LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
? ?? ?? ?? ? mDialogue = new TextView(context);??
? ?? ?? ?? ?mDialogue.setText(words);?
? ?? ?? ?? ?addView(mDialogue, new LinearLayout.LayoutParams(
? ?? ?? ?? ?? ?? ???LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
? ?? ???}?
? ?? ? /**?
? ?? ???* Convenience method to set the title of a SpeechView
? ?? ?? ?*/? ?? ?? ?
? ?? ? public void setTitle(String title) {??
? ?? ?? ?? ?? ?? ?? ? mTitle.setText(title);??
? ?? ? }? ?? ??
? ?? ?? ?/**
? ?? ?? ?* Convenience method to set the dialogue of a SpeechView?
? ?? ???*/??
? ?? ? public void setDialogue(String words) {
? ?? ?? ?? ?? ???mDialogue.setText(words);?
? ?? ???}??
? ?? ?}?
如果在其它的布局文件中要用到這個自定義的View,則在那個布局文件中在適當的位置處加入:
如在main.xml 中通過??
? ? <com.XX.CustomLayout android:id="@+id/youcustom"?
? ?? ???android:layout_width="XX"
? ?? ?? ?? ?android:layout_height="YY" />
然后在java代碼中通過CustomLayout layout=(CustomLayout)fatherView.FindViewById(R.id.youcustom)
實例化它
第一種是在擴展的LinearLayout構造函數中使用Inflater加載一個布局,并從中提取出相關的UI組件進行封裝,形成一個獨立的控件。在使用該控件時,由于它所有的子元素都是在運行時通過代碼動態創建的,所以該控件只能以一個獨立控件的形式在Layout文件中聲明,例如:
public class CustomLayout extends LinearLayout{
? ?? ? public??CustomLayout(Context context){
? ?? ?? ?? ?? ???LayoutInflater mInflater = LayoutInflater.from(context);
? ?? ?? ?? ?? ? View myView = mInflater.inflate(R.layout.receive, null);
? ?? ?? ?? ?? ? addView(myView);
? ?? ? }
}
< LinearLayout
??xmlns:android="http://schemas.android.com/apk/res/android"
??android:layout_width="wrap_content"
??android:layout_height="wrap_content"
??androidundefinedrientation="vertical" >
??<LinearLayout?
? ?? ?? ? android:layout_width="wrap_content"
? ?? ?? ? android:layout_height="wrap_content"
? ?? ?? ? androidundefinedrientation="horizontal">
? ?? ?? ? <TextView
? ?? ?? ?? ?? ?? ? android:layout_width="wrap_content"
? ?? ?? ?? ?? ?? ?android:layout_height="wrap_content" />
? ?? ?? ?<Button
? ?? ?? ?? ?? ???android:layout_width="wrap_content"
? ?? ?? ?? ?? ?? ?android:layout_height="wrap_content"?
? ?? ?? ?? ?? ?? ?android:id="@+id/button" />
??</LinearLayout>
??
? ?? ?? ???<TextView
? ?? ?? ?? ?? ?? ? android:layout_width="wrap_content"
? ?? ?? ?? ?? ?? ?android:layout_height="wrap_content" />? ?? ???
? ?? ?? ?? ?? ?? ??
< /LinearLayout>
第二種方式是:在自定義控件中聲明它的所有子元素,然后在Layout文件中像使用LinearLayout一樣去進行布局,不過切記:自定義控件Code中聲明的UI元素必須與Layout文件中聲明的元素保持一致,否則,在代碼中無法找到對應的控件;最后,在自定義控件的onInflateFinish中通過findViewById將layout中聲明的控件跟代碼中聲明的控件匹配起來,例如有某個自定義控件:public class CustomLayout extends LinearLayout{
? ? ListView??mListView; //代碼中聲明的控件
? ? XXXX; //針對UI控件封裝的操作
}
Layout文件中使用該布局進行聲明:
<com.XX.CustomLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? androidundefinedrientation="vertical" >
? ?? ???<ListView android:layout_width="wrap_content"? ?
? ?? ?? ? android:layout_height="fill_parent"? ?
? ?? ?? ? android:id="@+id/ListView01"??
? ?? ?? ? />
? ?? ???<.........>
< /com.XX.CustomLayout>
最后在代碼中進行匹配:
protected void onFinishInflate()? ?
? ? {? ?? ?? ?? ?? ??
? ?? ???mListView=(ListView)findViewById(R.id.ListView01);? ??
? ?? ???mListView.setAdapter(mAdapter);
? ?? ???mListView.setSelector(new ColorDrawable(Color.TRANSPARENT));
? ?? ???mListView.setBackgroundColor(0xF3EEE5);
? ?? ???mListView.setCacheColorHint(0);
? ?? ???mListView.setDivider(null);
? ?? ?? ?? ?
? ? }
第三種方式是:這個自定義VIEW中的任何控件都不是通過XML文件來定義的,而是在JAVA代碼中通過動態生成的,然后再addView()加入到你自定義的View中,
如:
private class SpeechView extends LinearLayout {??
? ?? ???private TextView mTitle;??
? ?? ???private TextView mDialogue;??
? ?? ???public SpeechView(Context context, String title, String words) {??
? ?? ?? ?? ?super(context);??
? ?? ?? ?? ?this.setOrientation(VERTICAL);??
? ?? ?? ?? ? // Here we build the child views in code. They could also have??
? ?? ?? ?? ?// been specified in an XML file.
? ?? ?? ?? ?mTitle = new TextView(context);
? ?? ?? ?? ?mTitle.setText(title);??
? ?? ?? ?? ?addView(mTitle, new LinearLayout.LayoutParams(??
? ?? ?? ?? ?? ?? ? LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
? ?? ?? ?? ? mDialogue = new TextView(context);??
? ?? ?? ?? ?mDialogue.setText(words);?
? ?? ?? ?? ?addView(mDialogue, new LinearLayout.LayoutParams(
? ?? ?? ?? ?? ?? ???LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
? ?? ???}?
? ?? ? /**?
? ?? ???* Convenience method to set the title of a SpeechView
? ?? ?? ?*/? ?? ?? ?
? ?? ? public void setTitle(String title) {??
? ?? ?? ?? ?? ?? ?? ? mTitle.setText(title);??
? ?? ? }? ?? ??
? ?? ?? ?/**
? ?? ?? ?* Convenience method to set the dialogue of a SpeechView?
? ?? ???*/??
? ?? ? public void setDialogue(String words) {
? ?? ?? ?? ?? ???mDialogue.setText(words);?
? ?? ???}??
? ?? ?}?
如果在其它的布局文件中要用到這個自定義的View,則在那個布局文件中在適當的位置處加入:
如在main.xml 中通過??
? ? <com.XX.CustomLayout android:id="@+id/youcustom"?
? ?? ???android:layout_width="XX"
? ?? ?? ?? ?android:layout_height="YY" />
然后在java代碼中通過CustomLayout layout=(CustomLayout)fatherView.FindViewById(R.id.youcustom)
實例化它
總結
以上是生活随笔為你收集整理的自定义控件例如LinearLayout 的三种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android自定义View的实现方法,
- 下一篇: android Button源码分析