首页 > 学院 > 开发设计 > 正文

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

2019-11-06 09:52:16
字体:
来源:转载
供稿:网友

其实自定义这种引入布局的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"/>
上一篇:float和double

下一篇:Glide的简单使用

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表