您的位置:首页 > 其它

自定义view-自定义布局中引入布局xml

2017-02-27 18:36 218 查看
其实自定义这种引入布局的view的好处就是将Activity中view的处理交给自定义view简化了Activity的代码处理,具体步骤如下。

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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐