一. 前言 常用来做子项。被一个自定义的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 ;private int layout_id = R.layout.item_layout;private ImageView icon_view;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 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(); } public XLItem (Context context, @Nullable AttributeSet attrs) { super (context, attrs); if (attrs != null ){ TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.XLItem); 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); 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 <?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 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 ; } } }); } }