riku
2025-10-17 fbae5f3ea74727ccadc48314a864a1ea0099a945
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package cn.flightfeather.thirdappmodule.view;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.flightfeather.thirdappmodule.R;
 
 
 
 
/**
 * 加载当前这个组合控件的布局文件
 * <p>
 * 加载自定义属性和组合控件中的元素进行绑定
 */
 
public class SettingCommItemView extends RelativeLayout {
 
    private FrameLayout leftContainer;
    private TextView leftTextView;
    private View divider;
 
    public SettingCommItemView(Context context) {
        super(context);
    }
 
    public SettingCommItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        // 加载当前这个组合控件的布局文件
        View.inflate(context, R.layout.setting_item, this);
 
 
        // 获取当前组合控件中所有的元素
        leftContainer = (FrameLayout) findViewById(R.id.left_container);
        leftTextView = (TextView) findViewById(R.id.left_text);
        divider = findViewById(R.id.divider);
 
 
        // 加载自定义属性
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingCommItemView);
 
        // 获取自定义属性的值
        String leftText = a.getString(R.styleable.SettingCommItemView_item_text);
        float leftTextSize = a.getDimension(R.styleable.SettingCommItemView_item_text_size, 18);
        int leftTextColor = a.getColor(R.styleable.SettingCommItemView_item_text_color, 0xff000000);
 
        Drawable lDrawable = a.getDrawable(R.styleable.SettingCommItemView_img_src);
 
        boolean flag = a.getBoolean(R.styleable.SettingCommItemView_is_divider_visibility, false);
 
 
        // 将组合控件中元素和自定义的属性值进行绑定
        leftTextView.setText(leftText);
 
        leftTextView.setTextSize(leftTextSize);
 
        leftTextView.setTextColor(leftTextColor);
 
        divider.setVisibility(flag ? VISIBLE : GONE);
 
        ImageView view;
        if (lDrawable != null) {
            view = new ImageView(context);
            view.setImageDrawable(lDrawable);
            leftContainer.addView(view);
        }
 
    }
 
    private void clearLeftView() {
        if (leftContainer.getChildCount() > 0) {
            leftContainer.removeAllViews();
        }
    }
 
 
 
    public void setLeftView(View v) {
        clearLeftView();
        leftContainer.addView(v);
    }
 
 
 
    public void setLeftText(CharSequence text) {
        leftTextView.setText(text);
    }
 
    public void setLeftTextColor(int color) {
        leftTextView.setTextColor(color);
    }
 
    public void setDividerVisibility(boolean visibility) {
        divider.setVisibility(visibility ? VISIBLE : INVISIBLE);
    }
}