一. 前言

常用来做子项。被一个自定义的View引用后,没有显示,可能的解决方案:自定义控件(15)—ViewGroup绘制的自定义子View的margin注意

Github地址:XLItem

二. 自定义View

1. 自定义用到的属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//资源
private int icon_id = R.drawable.ic_launcher_background;
//文本
private String title = "测试";

//正常状态的颜色
private int normal_color = Color.BLACK;
private int select_color = Color.MAGENTA;

//记录自身的索引
private int item_index = 0;

//布局id
private int layout_id = R.layout.item_layout;

//显示icon的视图
private ImageView icon_view;
//显示title的视图
private TextView title_view;

//按钮的状态-正常
public static final int STATUS_NORMAL = 0;
//按钮的状态-选中
public static final int STATUS_SELECT = 1;

2. 在values文件夹下自定义属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="XLItem">
<!--图标-->
<attr name="icon_id" format="reference"/>
<!--标题-->
<attr name="title" format="string"/>
<!--正常状态的颜色-->
<attr name="normal_color" format="color"/>
<!--选中状态的颜色-->
<attr name="select_color" format="color"/>
<!--布局文件-->
<attr name="layout_id" format="reference"/>
</declare-styleable>
</resources>

3. 创建构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* 构造方法:Java代码初始化
* @param context
*/
public XLItem(Context context, int icon_id, String title, int item_index, int normal_color, int select_color, int layout_id) {
super(context);

this.icon_id = icon_id;
this.title = title;
this.item_index = item_index;
this.normal_color = normal_color;
this.select_color = select_color;
if (layout_id != 0){
this.layout_id = layout_id;
}

//初始化操作
init();
}

/**
* 构造方法:Xml代码初始化
* @param context
* @param attrs
*/
public XLItem(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);

//1.获得所有属性值的集合
if (attrs != null){
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.XLItem);
//2.解析属性
icon_id = typedArray.getResourceId(R.styleable.XLItem_icon_id,icon_id);
title = typedArray.getString(R.styleable.XLItem_title);
normal_color = typedArray.getColor(R.styleable.XLItem_normal_color,normal_color);
select_color = typedArray.getColor(R.styleable.XLItem_select_color,select_color);
layout_id = typedArray.getResourceId(R.styleable.XLItem_layout_id,layout_id);

//3.释放资源
typedArray.recycle();
}

//初始化操作
init();
}

/**
* 初始化操作
*/
private void init() {
//找到控件
LinearLayout layout = (LinearLayout) LayoutInflater.from(getContext()).inflate(layout_id,this);
icon_view = layout.findViewById(R.id.icon);
title_view = layout.findViewById(R.id.title);

//设置数据
icon_view.setImageResource(icon_id);
title_view.setText(title);

//改为选中状态
changeStatus(STATUS_NORMAL);

setBackgroundColor(Color.TRANSPARENT);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!--item_layout.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>

<ImageView
android:id="@+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_launcher_background"
android:scaleType="fitXY"
/>

<TextView
android:id="@+id/title"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="测试"
android:textAlignment="center"
/>


</LinearLayout>

4. 改变状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 改变状态
* @param status
*/
public void changeStatus(int status){
//判断状态
if (status == STATUS_NORMAL){
icon_view.setColorFilter(normal_color);
title_view.setTextColor(normal_color);
}else {
icon_view.setColorFilter(select_color);
title_view.setTextColor(select_color);
}
}

三. 具体的使用

首先需要添加依赖,添加依赖之后,在布局文件中这样调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/root">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
>

<swu.xl.item.XLItem
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
app:title="测试"
/>

<swu.xl.item.XLItem
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
app:title="测试"

/>

<swu.xl.item.XLItem
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
app:title="测试"
/>

</LinearLayout>

</RelativeLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final XLItem item = new XLItem(this, R.drawable.ic_launcher_background, "测试", 0, Color.BLACK, Color.MAGENTA, R.layout.item_layout);

RelativeLayout layout = findViewById(R.id.root);
layout.addView(item);

final int[] flag = {0};
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag[0] == 0){
item.changeStatus(XLItem.STATUS_SELECT);

flag[0] = 1;
}else {
item.changeStatus(XLItem.STATUS_NORMAL);

flag[0] = 0;
}
}
});
}
}