安卓自定义标题栏

  1 import android.content.Context;
  2 import android.content.res.TypedArray;
  3 import android.graphics.drawable.Drawable;
  4 import android.util.AttributeSet;
  5 import android.view.LayoutInflater;
  6 import android.widget.ImageView;
  7 import android.widget.RelativeLayout;
  8 import android.widget.TextView;
  9 
 10 import androidx.annotation.ColorInt;
 11 import androidx.annotation.DrawableRes;
 12 import androidx.annotation.StringRes;
 13 
 14 import com.vocational.base.R;
 15 
 16 /**
 17  * 自定义的标题栏
 18  *
 19  * @author Silence
 20  * @since 1.0
 21  */
 22 public class CustomTitleView extends RelativeLayout {
 23 
 24     private ImageView leftImage;
 25     private TextView leftTitle;
 26     private TextView title;
 27     private TextView rightTitle;
 28     private ImageView rightImage;
 29 
 30     public CustomTitleView(Context context) {
 31         super(context);
 32     }
 33 
 34     public CustomTitleView(Context context, AttributeSet attrs) {
 35         super(context, attrs);
 36         init(context, attrs);
 37     }
 38 
 39     public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
 40         super(context, attrs, defStyleAttr);
 41         init(context, attrs);
 42     }
 43 
 44     private void init(Context context, AttributeSet attrs) {
 45         // 加载布局
 46         LayoutInflater.from(context).inflate(R.layout.custom_title_view_layout, this, true);
 47 
 48         // 初始化控件
 49         leftImage = findViewById(R.id.custom_title_view_left_image);
 50         leftTitle = findViewById(R.id.custom_title_view_left_title);
 51         title = findViewById(R.id.custom_title_view_title);
 52         rightTitle = findViewById(R.id.custom_title_view_right_title);
 53         rightImage = findViewById(R.id.custom_title_view_right_image);
 54 
 55         // 获取资源并设置
 56         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView);
 57 
 58         // 左侧图片
 59         Drawable leftSrc = typedArray.getDrawable(R.styleable.CustomTitleView_custom_title_view_left_src);
 60         // 左侧是否显示
 61         boolean leftVisibility = typedArray.getBoolean(R.styleable.CustomTitleView_custom_title_view_left_visibility, true);
 62         setLeftImageSrc(leftSrc);
 63         setLeftImageVisibility(leftVisibility);
 64 
 65         String leftTitleStr = typedArray.getString(R.styleable.CustomTitleView_custom_title_view_left_title);
 66         float leftTitleSize = typedArray.getDimension(R.styleable.CustomTitleView_custom_title_view_left_title_text_size, 12f);
 67         int leftTitleColor = typedArray.getColor(R.styleable.CustomTitleView_custom_title_view_left_title_text_color, 0x000000);
 68         setLeftTitleStr(leftTitleStr);
 69         setLeftTitleSize(leftTitleSize);
 70         setLeftTitleColor(leftTitleColor);
 71 
 72         String titleStr = typedArray.getString(R.styleable.CustomTitleView_custom_title_view_title);
 73         float titleSize = typedArray.getDimension(R.styleable.CustomTitleView_custom_title_view_title_text_size, 12f);
 74         int titleColor = typedArray.getColor(R.styleable.CustomTitleView_custom_title_view_title_text_color, 0x000000);
 75         setTitleStr(titleStr);
 76         setTitleSize(titleSize);
 77         setTitleColor(titleColor);
 78 
 79         String rightTitleStr = typedArray.getString(R.styleable.CustomTitleView_custom_title_view_right_title);
 80         float rightTitleSize = typedArray.getDimension(R.styleable.CustomTitleView_custom_title_view_right_title_text_size, 12f);
 81         int rightTitleColor = typedArray.getColor(R.styleable.CustomTitleView_custom_title_view_right_title_text_color, 0x000000);
 82         setRightTitleStr(rightTitleStr);
 83         setRightTitleSize(rightTitleSize);
 84         setRightTitleColor(rightTitleColor);
 85 
 86         // 右侧图片
 87         Drawable rightSrc = typedArray.getDrawable(R.styleable.CustomTitleView_custom_title_view_right_src);
 88         // 右侧是否显示
 89         boolean rightVisibility = typedArray.getBoolean(R.styleable.CustomTitleView_custom_title_view_right_visibility, false);
 90         setRightImageSrc(rightSrc);
 91         setRightImageVisibility(rightVisibility);
 92 
 93         // 释放资源
 94         typedArray.recycle();
 95     }
 96 
 97     public void setLeftImageSrc(@DrawableRes int imageSrc) {
 98         if (leftImage != null) {
 99             leftImage.setImageResource(imageSrc);
100         }
101     }
102 
103     public void setLeftImageSrc(Drawable imageSrc) {
104         if (leftImage != null) {
105             leftImage.setImageDrawable(imageSrc);
106         }
107     }
108 
109     public void setLeftImageVisibility(boolean visibility) {
110         if (leftImage != null) {
111             leftImage.setVisibility(visibility ? VISIBLE : GONE);
112         }
113     }
114 
115     public void setLeftTitleStr(CharSequence str) {
116         if (leftTitle != null) {
117             leftTitle.setText(str);
118         }
119     }
120 
121     public void setLeftTitleStr(@StringRes int str) {
122         if (leftTitle != null) {
123             leftTitle.setText(str);
124         }
125     }
126 
127     public void setLeftTitleSize(float size) {
128         if (leftTitle != null) {
129             leftTitle.getPaint().setTextSize(size);
130         }
131     }
132 
133     public void setLeftTitleColor(@ColorInt int color) {
134         if (leftTitle != null) {
135             leftTitle.setTextColor(color);
136         }
137     }
138 
139     public void setTitleStr(CharSequence str) {
140         if (title != null) {
141             title.setText(str);
142         }
143     }
144 
145     public void setTitleStr(@StringRes int str) {
146         if (title != null) {
147             title.setText(str);
148         }
149     }
150 
151     public void setTitleSize(float size) {
152         if (title != null) {
153             title.getPaint().setTextSize(size);
154         }
155     }
156 
157     public void setTitleColor(@ColorInt int color) {
158         if (title != null) {
159             title.setTextColor(color);
160         }
161     }
162 
163     public void setRightTitleStr(CharSequence str) {
164         if (rightTitle != null) {
165             rightTitle.setText(str);
166         }
167     }
168 
169     public void setRightTitleStr(@StringRes int str) {
170         if (rightTitle != null) {
171             rightTitle.setText(str);
172         }
173     }
174 
175     public void setRightTitleSize(float size) {
176         if (rightTitle != null) {
177             rightTitle.getPaint().setTextSize(size);
178         }
179     }
180 
181     public void setRightTitleColor(@ColorInt int color) {
182         if (rightTitle != null) {
183             rightTitle.setTextColor(color);
184         }
185     }
186 
187     public void setRightImageSrc(@DrawableRes int imageSrc) {
188         if (rightImage != null) {
189             rightImage.setImageResource(imageSrc);
190         }
191     }
192 
193     public void setRightImageSrc(Drawable imageSrc) {
194         if (rightImage != null) {
195             rightImage.setImageDrawable(imageSrc);
196         }
197     }
198 
199     public void setRightImageVisibility(boolean visibility) {
200         if (rightImage != null) {
201             rightImage.setVisibility(visibility ? VISIBLE : GONE);
202         }
203     }
204 }
View类

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:minHeight="36dp"
 7     android:paddingStart="16dp"
 8     android:paddingEnd="16dp">
 9 
10     <ImageView
11         android:id="@+id/custom_title_view_left_image"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:layout_centerVertical="true"
15         android:src="@mipmap/app_top_left_back_logo" />
16 
17     <TextView
18         android:id="@+id/custom_title_view_left_title"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:layout_alignWithParentIfMissing="true"
22         android:layout_centerVertical="true"
23         android:layout_marginStart="10dp"
24         android:layout_toEndOf="@id/custom_title_view_left_image"
25         android:visibility="gone" />
26 
27     <TextView
28         android:id="@+id/custom_title_view_title"
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content"
31         android:layout_alignWithParentIfMissing="true"
32         android:layout_centerVertical="true"
33         android:layout_toStartOf="@id/custom_title_view_right_title"
34         android:layout_toEndOf="@id/custom_title_view_left_title"
35         android:gravity="center" />
36 
37     <TextView
38         android:id="@+id/custom_title_view_right_title"
39         android:layout_width="wrap_content"
40         android:layout_height="wrap_content"
41         android:layout_alignWithParentIfMissing="true"
42         android:layout_centerVertical="true"
43         android:layout_marginEnd="10dp"
44         android:layout_toStartOf="@id/custom_title_view_right_image"
45         android:visibility="gone" />
46 
47     <ImageView
48         android:id="@+id/custom_title_view_right_image"
49         android:layout_width="wrap_content"
50         android:layout_height="wrap_content"
51         android:layout_alignWithParentIfMissing="true"
52         android:layout_alignParentEnd="true"
53         android:layout_centerVertical="true"
54         android:visibility="gone" />
55 </RelativeLayout>
XML文件

 

 1 <declare-styleable name="CustomTitleView">
 2         <!--        左侧图片资源-->
 3         <attr name="custom_title_view_left_src" format="reference" />
 4         <!--        左侧图片资源是否显示-->
 5         <attr name="custom_title_view_left_visibility" format="boolean" />
 6         <!--        左侧的标题-->
 7         <attr name="custom_title_view_left_title" format="string|reference" />
 8         <!--        左侧的标题大小-->
 9         <attr name="custom_title_view_left_title_text_size" format="dimension" />
10         <!--        左侧的标题颜色-->
11         <attr name="custom_title_view_left_title_text_color" format="color" />
12         <!--        右侧的标题-->
13         <attr name="custom_title_view_right_title" format="string|reference" />
14         <!--        右侧的标题大小-->
15         <attr name="custom_title_view_right_title_text_size" format="dimension" />
16         <!--        右侧的标题颜色-->
17         <attr name="custom_title_view_right_title_text_color" format="color" />
18         <!--        标题-->
19         <attr name="custom_title_view_title" format="string|reference" />
20         <!--        标题大小-->
21         <attr name="custom_title_view_title_text_size" format="dimension" />
22         <!--        标题颜色-->
23         <attr name="custom_title_view_title_text_color" format="color" />
24         <!--        右侧图片资源-->
25         <attr name="custom_title_view_right_src" format="reference" />
26         <!--        右侧图片资源是否显示-->
27         <attr name="custom_title_view_right_visibility" format="boolean" />
28     </declare-styleable>
自定义属性

 

posted @ 2020-05-26 18:19  CoderSilence  阅读(176)  评论(0编辑  收藏  举报