简介
这段为了工作的需要写了下自定义的组合控件,因为页面比较多而且还非常相识,为了
再xml里面用的方便就用了自定义的组合控件,下面为大家那个例子简单的给大家介绍
一下,应该看完我的实例以后大家对自定义组合控件会有更一步的了解,我会给大家
分步骤讲解:
第一步
首先我们就是先定义一个我们想要的布局样式,今天我就拿我们最常见的一条前后
有字加横线为例,下面是我在xml文件定义的样式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_left"
android:textSize="14sp"
android:textColor="#000"
android:layout_marginLeft="15dp"
android:text="身份证照片"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_right"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:textSize="12sp"
android:textColor="#929292"
android:text="最多上传6张照片"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
<View
android:background="#929292"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</LinearLayout>
第二步
我们就开始创建一个class类继承Relativelayout代码如下:
public class ItemTextView extends RelativeLayout {
String left_text;
String right_text;
private TextView tv_left;
private TextView tv_right;
int tvcolor_left;
int tvcolor_right;
public ItemTextView(Context context) {
super(context);
// 初始化方法
initView(context);
}
public ItemTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// 初始化自定义属性
initTypedArray(context,attrs);
// 初始化方法
initView(context);
}
public ItemTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 初始化自定义属性
initTypedArray(context,attrs);
initView(context);
}
// 自定义的方法来查找自定义的属性
public void initTypedArray(Context context,AttributeSet attrs){
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemTextView);
left_text=typedArray.getString(R.styleable.ItemTextView_text_left);
right_text=typedArray.getString(R.styleable.ItemTextView_text_right);
tvcolor_left=typedArray.getColor(R.styleable.ItemTextView_textColor_left,Color.BLACK);
tvcolor_right=typedArray.getColor(R.styleable.ItemTextView_textColor_right,Color.YELLOW);
// 资源及时回收
typedArray.recycle();
}
// 初始化方法找控件和控件id并赋值给id
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.camear,this,true);
tv_left=findViewById(R.id.tv_left);
tv_right= findViewById(R.id.tv_right);
// 自定义属性赋值
tv_left.setTextColor(tvcolor_left);
tv_right.setTextColor(tvcolor_right);
setTvLeft(left_text);
setTvRight(right_text);
}
// 对外提供一个动态赋值的方法,让控件在Activity里面可以赋值
public void setLeftTextColor(int color){
tv_left.setTextColor(color);
}
public void setRightTexyColor(int color){
tv_right.setTextColor(color);
}
public void setTvLeft(String text){
if (!TextUtils.isEmpty(text)){
tv_left.setText(text);
}
}
public void setTvRight(String text1){
if (!TextUtils.isEmpty(text1)){
tv_right.setText(text1);
}
}
//提供对外的点击事件,可以实现每个控件的点击
public void textLeftListener(OnClickListener onClickListener){
tv_left.setOnClickListener(onClickListener);
}
public void textRightListener(OnClickListener onClickListener){
tv_right.setOnClickListener(onClickListener);
}
}
第三步
就是我们要在values里面自定义属性代码如下:
<declare-styleable name="ItemTextView">
<attr name="text_left" format="string"/>
<attr name="text_right" format="string"/>
<attr name="textColor_left" format="color"/>
<attr name="textColor_right" format="color"/>
</declare-styleable>
第四步
这下我们的自定义组合控件就算是完成了,我们直接去调用就可以了。
在mainactivity.xml里面就可以调用了,代码如下:
<com.weizhen.myzuhe.ItemTextView
android:id="@+id/item_tvv"
appp:text_left="我是左边"
appp:text_right="我是右边"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.weizhen.myzuhe.ItemTextView>
第五步
就是我们就可以在Activity里面调用了,代码如下:
public class MainActivity extends AppCompatActivity {
private ItemTextView item_tvv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化数据
initView();
}
private void initView() {
item_tvv = (ItemTextView) findViewById(R.id.item_tvv);
item_tvv.setLeftTextColor(Color.RED);
// 点击事件
onListevens();
}
private void onListevens() {
item_tvv.textLeftListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"左边",Toast.LENGTH_SHORT).show();
}
});
item_tvv.textRightListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"右边",Toast.LENGTH_SHORT).show();
}
});
}
}
下面是额外补助:
自定义属性: 是指定义可以在布局文件的标签中使用的属性。如TextView控件中的Text属性,但是它是由系统提供的,现如今是由我们自己定义。
使用自定义视图属性的好处: 这样就可以通过布局xml的方式给视图对象指定自己定义的任意属性值, 而不是仅仅只能使用系统中内定的属性啦。
属性值的类型(format)有如下几种:
1、reference 引用类型值 : @id/...
2、 color 颜色类型值 #ff00ff
3、 boolean 布尔类型值 true , false
4、 dimension 尺寸类型值 dp / px /sp
5、 integer 整数类型值 weight progress max
6、float 浮点型值 0.1f
7、string 字符串类型值 "atrrs"
8、<enum> 枚举类型值 :水平/垂直
9、 flag:位或运算
10、fraction:百分数