自定义控件例如LinearLayout 的三种方法
来源:互联网 发布:放置江湖修改数据 编辑:程序博客网 时间:2024/05/21 11:04
最近通过看别人代码和网上搜索,发现现在自定义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)
实例化它
0 0
- 自定义控件例如LinearLayout 的三种方法
- 自定义控件例如LinearLayout 的三种方法
- 自定义控件例如LinearLayout 的三种方法
- Android自定义LinearLayout的三种方法
- 继承linearLayout实现自定义布局的方法
- 自定义控件三实现方法
- Android自定义控件---继承Linearlayout
- Android拖动控件的实现,自定义可拖动的LinearLayout
- android 自定义控件的三种方式
- 自定义LinearLayout的几种方式
- 自定义可拖拽的LinearLayout
- 简单的自定义控件三
- 【android自定义控件】LinearLayout定义ActionBar样式
- 自定义控件,从LinearLayout源码谈起
- Anroid自定义控件/继承自ViewGroup的刷新控件——LinearLayout
- 在自定义的xml里面的控件, extends LinearLayout的情况 ,会出现 方法一 加载不出图片的情况
- Qt自定义控件的两种方法
- Android-自定义控件的两种方法
- PowerBuilder
- 关于CSDN无法编辑自己简介的问题
- python实现嵌套列表、字典按某一元素去重复
- VisualC# winform窗体应用程序 语句加this有的怎么不加?
- Click事件在显示和隐藏中的BUG
- 自定义控件例如LinearLayout 的三种方法
- 【Android】 加解密算法 HMAC 的使用
- 第<3>章 Typeglob和符号表
- Android加载Gif图片的一般方法:Movie实现
- Zoho Books的库存功能介绍
- android基础:android页面添加滚动条
- tyvj p1008 传球游戏
- 前端颜色版不错的工具 收藏掉
- 开始学习Python