效果图
1 import android.content.Context; 2 import android.content.res.TypedArray; 3 import android.graphics.Color; 4 import android.graphics.drawable.Drawable; 5 import android.util.AttributeSet; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.ImageView; 10 import android.widget.LinearLayout; 11 import android.widget.TextView; 12 13 import androidx.annotation.ColorInt; 14 import androidx.annotation.Nullable; 15 import androidx.annotation.StringRes; 16 17 /** 18 * 自定义上下结构的view 上部为图片或者文字 下部是标题 19 * 可以拓展 20 * 21 * @author Silence 22 * @version 1.0 23 */ 24 public class CustomVerticalTitleView extends LinearLayout { 25 26 private LinearLayout parentLayout; 27 /** 28 * 顶部显示的标题view 29 */ 30 private TextView topTextView; 31 /** 32 * 上部标题颜色 33 */ 34 private int topTextColor; 35 /** 36 * 上部标题 37 */ 38 private String topText; 39 /** 40 * 上部标题字体大小 41 */ 42 private float topTextSize; 43 /** 44 * 顶部图标 45 */ 46 private ImageView topImageView; 47 /** 48 * 顶部图标是否显示 默认不显示 49 */ 50 private boolean imageViewVisibility = false; 51 /** 52 * 顶部图片drawable 53 */ 54 private Drawable topImageSrc; 55 /** 56 * 顶部图片宽度 57 */ 58 private float topImageWidth; 59 /** 60 * 顶部图片高度 61 */ 62 private float topImageHeight; 63 /** 64 * 底部标题view 65 */ 66 private TextView titleTextView; 67 /** 68 * 底部标题颜色 69 */ 70 private int titleTextColor; 71 /** 72 * 底部标题 73 */ 74 private String titleText; 75 /** 76 * 底部标题字体大小 77 */ 78 private float titleTextSize; 79 /** 80 * 上下间距 81 */ 82 private float verticalMarginTop; 83 84 public CustomVerticalTitleView(Context context) { 85 super(context); 86 } 87 88 public CustomVerticalTitleView(Context context, @Nullable AttributeSet attrs) { 89 super(context, attrs); 90 initView(context, attrs); 91 } 92 93 public CustomVerticalTitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 94 super(context, attrs, defStyleAttr); 95 initView(context, attrs); 96 } 97 98 public CustomVerticalTitleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 99 super(context, attrs, defStyleAttr, defStyleRes); 100 initView(context, attrs); 101 } 102 103 private void initView(Context context, AttributeSet attrs) { 104 //加载布局 105 LayoutInflater.from(context).inflate(R.layout.custom_vertical_title_view_layout, this, true); 106 107 // 初始化控件 108 parentLayout = findViewById(R.id.custom_vertical_view_parent); 109 topTextView = findViewById(R.id.custom_vertical_title_top_text_view); 110 topImageView = findViewById(R.id.custom_vertical_title_top_image_view); 111 titleTextView = findViewById(R.id.custom_vertical_title_text_view); 112 113 // 获取资源 114 getResources(context, attrs); 115 } 116 117 /** 118 * 获取资源设置 119 */ 120 private void getResources(Context context, AttributeSet attrs) { 121 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomVerticalTitleView); 122 123 // 上部文字 124 topText = typedArray.getString(R.styleable.CustomVerticalTitleView_vertical_view_top_title); 125 topTextColor = typedArray.getColor(R.styleable.CustomVerticalTitleView_vertical_view_top_title_color, getResources().getColor(R.color.colorBlack)); 126 topTextSize = typedArray.getDimension(R.styleable.CustomVerticalTitleView_vertical_view_top_title_text_size, 0); 127 128 // 上部图片 129 topImageSrc = typedArray.getDrawable(R.styleable.CustomVerticalTitleView_vertical_view_top_image_src); 130 topImageWidth = typedArray.getDimension(R.styleable.CustomVerticalTitleView_vertical_view_top_image_width, 0); 131 topImageHeight = typedArray.getDimension(R.styleable.CustomVerticalTitleView_vertical_view_top_image_height, 0); 132 133 // 底部标题 134 titleText = typedArray.getString(R.styleable.CustomVerticalTitleView_vertical_view_title); 135 titleTextColor = typedArray.getColor(R.styleable.CustomVerticalTitleView_vertical_view_title_color, getResources().getColor(R.color.colorBlack)); 136 titleTextSize = typedArray.getDimension(R.styleable.CustomVerticalTitleView_vertical_view_title_text_size, 0); 137 138 verticalMarginTop = typedArray.getDimension(R.styleable.CustomVerticalTitleView_vertical_view_vertical_margin_top, 0); 139 140 imageViewVisibility = typedArray.getBoolean(R.styleable.CustomVerticalTitleView_vertical_view_image_visibility, false); 141 142 initWidget(context); 143 // 回收资源 144 typedArray.recycle(); 145 } 146 147 /** 148 * 初始化 149 * 150 * @param context 上下文 151 */ 152 private void initWidget(Context context) { 153 // 初始化上部图标和文字的显示状态 154 topImageView.setVisibility(imageViewVisibility ? View.VISIBLE : GONE); 155 topTextView.setVisibility(imageViewVisibility ? View.GONE : VISIBLE); 156 157 if (topTextView != null) { 158 topTextView.setText(topText); 159 topTextView.setTextColor(topTextColor); 160 topTextView.getPaint().setTextSize(topTextSize); 161 } 162 163 if (topImageView != null) { 164 if (topImageSrc != null) { 165 topImageView.setImageDrawable(topImageSrc); 166 } 167 168 LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 169 if (topImageWidth > 0) { 170 layoutParams.width = (int) topImageWidth; 171 } 172 173 if (topImageHeight > 0) { 174 layoutParams.height = (int) topImageHeight; 175 } 176 topImageView.setLayoutParams(layoutParams); 177 } 178 179 if (titleTextView != null) { 180 titleTextView.setText(titleText); 181 titleTextView.setTextColor(titleTextColor); 182 titleTextView.getPaint().setTextSize(titleTextSize); 183 LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 184 if (verticalMarginTop >= 0) { 185 layoutParams.setMargins(0, (int) verticalMarginTop, 0, 0); 186 } 187 titleTextView.setLayoutParams(layoutParams); 188 } 189 } 190 191 /** 192 * 设置顶部text 193 * 194 * @param text 文字 195 */ 196 public final void setTopText(String text) { 197 topTextView.setText(text); 198 199 } 200 201 /** 202 * 设置顶部text颜色 203 * 204 * @param color 颜色值 205 */ 206 public final void setTopTextColor(int color) { 207 topTextView.setTextColor(color); 208 } 209 210 /** 211 * 设置顶部textSize 212 * 213 * @param size 字体大小 214 */ 215 public final void setTopTextSize(float size) { 216 topTextView.getPaint().setTextSize(size); 217 } 218 219 /** 220 * 设置顶部图片是否显示 221 * 222 * @param visibility 是否显示 true 显示 false 不显示 223 */ 224 public final void setImageViewVisibility(boolean visibility) { 225 topImageView.setVisibility(visibility ? VISIBLE : GONE); 226 } 227 228 /** 229 * 设置顶部图片是否显示 230 * 231 * @param visibility 直接设置visibility 232 */ 233 public final void setImageViewVisibility(int visibility) { 234 topImageView.setVisibility(visibility); 235 } 236 237 /** 238 * 设置底部title文字 239 * 240 * @param text 要设置的文字 241 */ 242 public final void setTitleText(String text) { 243 titleTextView.setText(text); 244 } 245 246 /** 247 * 设置底部title颜色 248 * 249 * @param color 要设置的颜色 250 */ 251 public final void setTitleTextColor(int color) { 252 titleTextView.setTextColor(color); 253 } 254 255 /** 256 * 设置底部title文字大小 257 * 258 * @param size 大小 259 */ 260 public final void setTitleTextSize(float size) { 261 titleTextView.getPaint().setTextSize(size); 262 } 263 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/custom_vertical_view_parent" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:gravity="center" 7 android:orientation="vertical"> 8 9 <TextView 10 android:id="@+id/custom_vertical_title_top_text_view" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" /> 13 14 <ImageView 15 android:id="@+id/custom_vertical_title_top_image_view" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:visibility="gone" /> 19 20 <TextView 21 android:id="@+id/custom_vertical_title_text_view" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" /> 24 25 </LinearLayout>
1 <declare-styleable name="CustomVerticalTitleView"> 2 <!-- 顶部标题--> 3 <attr name="vertical_view_top_title" format="reference|string" /> 4 <!-- 顶部标题颜色--> 5 <attr name="vertical_view_top_title_color" format="color" /> 6 <!-- 顶部标题字体大小--> 7 <attr name="vertical_view_top_title_text_size" format="dimension" /> 8 <!-- 顶部图片--> 9 <attr name="vertical_view_top_image_src" format="reference" /> 10 <!-- 顶部图片宽度--> 11 <attr name="vertical_view_top_image_width" format="dimension" /> 12 <!-- 顶部图片高度--> 13 <attr name="vertical_view_top_image_height" format="dimension" /> 14 <!-- 标题--> 15 <attr name="vertical_view_title" format="reference|string" /> 16 <!-- 标题颜色--> 17 <attr name="vertical_view_title_color" format="color" /> 18 <!-- 标题字体大小--> 19 <attr name="vertical_view_title_text_size" format="dimension" /> 20 <!-- 上下间距--> 21 <attr name="vertical_view_vertical_margin_top" format="dimension" /> 22 <!-- 上下间距--> 23 <attr name="vertical_view_image_visibility" format="boolean" /> 24 </declare-styleable>