自定义view-自定义布局中引入布局xml
2017-02-27 18:36
218 查看
其实自定义这种引入布局的view的好处就是将Activity中view的处理交给自定义view简化了Activity的代码处理,具体步骤如下。
1.首先创建需要引入布局的xml代码如下
2.之后根据我们需要自定义的内容编写自定义布局逻辑处理代码如下
关键代码LayoutInflater.from(context).inflate(R.layout.item_child, this);这样的话就将布局引入了我们自定义控件中就可以处理控件的逻辑了
调用也是是跟简单的代码如下
1.首先创建需要引入布局的xml代码如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" > <ImageView android:id="@+id/image_jian" android:layout_marginTop="2dp" android:src="@drawable/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/editText" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/image_jian" android:includeFontPadding="false" android:text="11" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
2.之后根据我们需要自定义的内容编写自定义布局逻辑处理代码如下
public class MyChildView extends LinearLayout { ImageView image_jian; EditText editText; public MyChildView(Context context) { this(context, null); } public MyChildView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.item_child, this); editText = (EditText) findViewById(R.id.editText); image_jian = (ImageView) findViewById(R.id.image_jian); image_jian.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { editText.setText(""); } }); } }
关键代码LayoutInflater.from(context).inflate(R.layout.item_child, this);这样的话就将布局引入了我们自定义控件中就可以处理控件的逻辑了
调用也是是跟简单的代码如下
<com.testview.testChlidView.MyChildView android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="match_parent"/>
相关文章推荐
- Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题
- Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题
- Android自定义View研究(七)--XML中布局自定义View时View触摸原点问题
- 自定义TextView,在values下面新建attrs.xml,布局,重写方法
- Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题
- Android自定义View研究(七)--XML中布局自定义View时View触摸原点问题
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- Android布局文件.xml中的自定义属性(结合封装的自定义View)
- Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题
- ListView 使用自定义适配器,将自定义xml布局转换成view调整布局
- Android学习个人笔记1-Layout界面布局-xml,java混合模式-自定义view
- 自定义View时,如何设置字体的大小和间距,和XML布局达到一样的效果
- Android自定义View研究(七)--XML中布局自定义View时View触摸原点问题
- Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题
- xml布局自定义SurfaceView模板
- xml布局自定义SurfaceView模板
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- Android 自定义View引用xml布局
- Android自定义View研究(七)--XML中布局自定义View时View触摸原点问题
- xml布局自定义SurfaceView模板