本文是一个教程文,学完后,会做出一个简单的自定义控件,至于更复杂的自定义控件,原理都交给你了,怎么做就看自己的大脑了。
这个简单的自定义控件的效果就是一个两个文本框,点击上面文本框,下面文本框显示会改变。
首先需要三个文件:
xxxActivity.java文件+xxx.xml文件+attrrs.xml文件
先说一下三个文件的作用:
xxxActivity的作用:
定义自定义控件中需要被用户控制的组件以及变量,甚至一些用户可能会用到的方法;
xxx.xml的作用:
编写用户需要的UI;
attrrs.xml的作用:
编写用在xml文件中的控制组件的自定义的属性
然后上代码,上一个代码解释一个。
xml文件的代码就是简单的放进去两个TextView:
zdy_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:id="@+id/zdy_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1111111" />
<View
android:id="@+id/zdy_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="look" />
</LinearLayout>
然后是在上attrs.xml文件(这个就是你用在xml里面的自定义属性):
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name=ZdyLayout">
<attr name="click" format="string" />
</declare-styleable>
</resources>
这里简单解释一下,这就是定义了一个自定义的xml文件里面属性,属性名叫click,类型是string的,存储这个属性串的xml的别名叫做ZdyLayout。
接下来重头戏,上类文件:
ZdyLayout.java:
public class ZdyLayout extends FrameLayout {
private View mView;
private TextView tv1;
private TextView tv2;
private void init(Context context,AttributeSet attrs){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.zdy_activity,this,true);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MenuItemLayout);
tv1 = mView.findViewById(R.id.zdy_tv1);
tv2 = mView.findViewById(R.id.zdy_tv2);
tv1.setText(a.getString(R.styleable.MenuItemLayout_click));
}
//tv1和tv2的setget方法,这里省略;
//下面是继承FrameLayout后需要实现的方法,
//实现四个的话,参数最多的那个不用改变,实现三个的话,就是下面的代码
public ZdyLayout(Context context){
this(context,null);
}
public ZdyLayout(Context context,AttributeSet attrs){
this(context,attrs,0);
}
public ZdyLayout(Context context,AttrubuteSet attrs,int defStyleAttr){
super(context,attrs,defStyleAttr);
init(context,attrs);
}
}
至此,自定义控件完成,那么怎么使用呢?
先写一个程序入口类的布局文件,在这个文件中使用这个控件,下面方法使用这个控件的代码:
<com.myth.zframe.zidingyi.ZdyLayout
android:id="@id/test"
app:click="点我"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
上面代码很简单,但是多了一个app:click这个属性,这个就是上面attrs里面我们自定义的属性,我们为这个属性传入了一个“点我” 的字符串。
然后我们看一下我们的ZdyLayout中init方法:
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MenuItemLayout);
这一句将attrs里面的定义拿出来;
tv1.setText(a.getString(R.styleable.MenuItemLayout_click));
这一句去使用这个属性的传入值。
现在明白自定义控件的自定义属性怎么编写了吧。
最后我们来写一个程序入口类:
public class MainActivity extends AppCompatActivity{
private ZdyLayout layout;
@Override
protected void OnCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.zdy_activity);
initRes();
}
private void initRes(){
layout = findeViewById(R.id.test);
layout.getTv1().setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View view){
layout.gettv2.setText("2222");
}
}):
}
}
至此,使用自定义控件也已经结束,我们运行一下点击“点我”,发现下面的TextView变成了2222,
这就是我们写的简单的自定义控件。