之所以用得到这个技术,主要是在实际的项目开发中要反复去使用某个控件来实现文件选择的功能。因此想到了自定义控件的方法,将需要复用的控件及其事件响应统一封装:
好吧没接下来进入主题:
参考别人的博客介绍实现linearLayout的三种方式
方式1
在Linearlayout的构造函数中通过使用映射机制加载布局文件,即通过Inflater方法,在使用该方法自定义的控件时,由于所有的子元素都是在运行时通过代码动态创建的,所以该控件只能以一个独立的控件形式在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>
方式2
在自定义控件中声明它的所有子元素,然后在Layout文件中像使用LinearLayout一样去进行布局,不过切记:自定义控件Code中声明的UI元素必须与Layout文件中声明的元素保持一致,否则,在代码中无法找到对应的控件;最后,在自定义控件的onInflateFinish中通过findViewById将layout中声明的控件跟代码中声明的控件匹配起来
特别标注下:onInflateFinish
该方法据资料介绍,是在oncreat方法执行之间就已经执行了。。。。并不是很理解这句话的意思,总之我知道它执行的比较早就得了,目前还不知道早执行在我的工程中有个ruan用(妹子的打个ruan字半天没找到),相信以后可能会用到
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>
注意:此方法应该注意的是在使用该自定义控件的时候,在布局文件中引入了该控件。与原生的控件区别是 有了包名。。在activity中实际使用的时候,没发现什么大的区别,加上id属性在获取实例具体使用等方面都一样
可能需要注意的地方是在实现自定义控件的过程中一下。。。。。。
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);
}
方式3
控件中的view和以上两种方式的生成方式不同,是直接在代码中通过代码动态生成的
然后就需要再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);
}
}
自定义空间完成以后具体如何使用呢:》》》》》
我总结为不要把自定义弓箭看成是异类,直接跟原来使用原生控件一样,在引入layout中需要注意引入包名就ok了
例子
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.sun.sunfilechoose.AddAttachmentsLayout
android:id="@+id/custom_add_attachments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</com.sun.sunfilechoose.AddAttachmentsLayout>
</LinearLayout>