preface

The purpose of this article is to document the requirements of business controls, products, and you never know what they will need next.

Introduction to the

  1. Lead the text.
  2. With tail icon.
  3. The required text is displayed in the middle.
  4. XML control header text style is supported.
  5. Support for XML control intermediate text styles.
  6. Support for XML control tail ICONS

rendering

Method of use

<! -- Real name authentication -->
<com.momin.common.widget.SelectTextView
    android:id="@+id/stv_certification_detail_car_sim"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    app:headerText="@string/certification_detail_car_sim_tips"
    android:text="@string/certification_detail_car_sim_false"
    android:textColor="@color/c_5D82A6"/>
Copy the code

The source code in this

Business controls, commented in detail, will not be explained further.

Attrs.xml attribute definition

<! -- Public properties -->
<! -- Prefacing text -->
<attr name="headerText" format="string"/>
<! -- Font size -->
<attr name="headerTextSize" format="dimension"/>
<! -- Font size -->
<attr name="headerTextStyle">
    <flag name="normal" value="0" />
    <flag name="bold" value="1" />
    <flag name="italic" value="2" />
</attr>
<! -- Text color -->
<attr name="headerTextColor" format="reference|color"/>
<! -- Left margin of leading text -->
<attr name="headerPaddingStart" format="dimension"/>
<! -- Right margin of front text -->
<attr name="headerPaddingEnd" format="dimension"/>
<! -- Top spacing of prefacing text -->
<attr name="headerPaddingTop" format="dimension"/>
<! -- Bottom spacing of front text -->
<attr name="headerPaddingBottom" format="dimension"/>
<! -- Public properties -->

<! -- Text display box with front text and back icon -->
<declare-styleable name="SelectTextView">
    <! -- Text content -->
    <attr name="android:text"/>
    <! -- Text size -->
    <attr name="android:textSize"/>
    <! -- Text color -->
    <attr name="android:textColor"/>
    <! -- Alignment -->
    <attr name="android:gravity"/>
    <! -- Text start margin -->
    <attr name="textMarginStart" format="dimension"/>
    <! -- End of text margin -->
    <attr name="textMarginEnd" format="dimension"/>
    <! -- Text top margin -->
    <attr name="textMarginTop" format="dimension"/>
    <! -- Bottom margin of text -->
    <attr name="textMarginBottom" format="dimension"/>
    <! -- Text start spacing -->
    <attr name="textPaddingStart" format="dimension"/>
    <! -- End of text spacing -->
    <attr name="textPaddingEnd" format="dimension"/>
    <! -- Top spacing of text -->
    <attr name="textPaddingTop" format="dimension"/>
    <! -- Bottom spacing of text -->
    <attr name="textPaddingBottom" format="dimension"/>
    <! -- Prompt text -->
    <attr name="android:hint"/>
    <! -- Prompt text color -->
    <attr name="android:textColorHint"/>
    <! -- Prefacing text -->
    <attr name="headerText"/>
    <! -- Font size -->
    <attr name="headerTextSize"/>
    <! -- Font size -->
    <attr name="headerTextStyle"/>
    <! -- Text color -->
    <attr name="headerTextColor"/>
    <! -- Left margin of leading text -->
    <attr name="headerPaddingStart"/>
    <! -- Right margin of front text -->
    <attr name="headerPaddingEnd"/>
    <! -- Top spacing of prefacing text -->
    <attr name="headerPaddingTop"/>
    <! -- Bottom spacing of front text -->
    <attr name="headerPaddingBottom"/>
    <! -- Back icon -->
    <attr name="endIcon" format="reference" />
    <! -- Whether the rear icon is displayed -->
    <attr name="endIconVisible" format="boolean" />
</declare-styleable>
Copy the code

Common_textview_select. XML UI layout


      
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <! -- Header text -->
    <TextView
        android:id="@+id/tv_text_select_header"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <! -- Middle content -->
    <TextView
        android:id="@+id/tv_text_select_content"
        app:layout_constraintStart_toEndOf="@id/tv_text_select_header"
        app:layout_constraintEnd_toStartOf="@id/tv_text_select_icon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:singleLine="true"
        android:ellipsize="end"/>
    <! -- Tail icon -->
    <ImageView
        android:id="@+id/tv_text_select_icon"
        android:layout_marginStart="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/common_arrow_right_gray"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code

Selecttextview. Java custom control source

package com.momin.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.momin.common.R;

/** * <p>Title: SelectTextView</p> * <p>Description: Copyright: </p> * <p>Company: </p> **@author Momin
 * @version 1.0
 * @date2021/3/17 * / a quarter to ten

public class SelectTextView extends RelativeLayout {

    private TextView tvHead;
    private TextView tvContent;
    private ImageView ivEndIcon;

    public SelectTextView(Context context) {
        super(context);
        init(context, null);
    }

    public SelectTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SelectTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /** * initializes **@paramContext *@paramAttrs Resource object */
    private void init(Context context, AttributeSet attrs) {
        initView(context);

        // Get the resource object
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelectTextView);

        // Initialize the header text
        CharSequence headText = typedArray.getText(R.styleable.SelectTextView_headerText);
        if(! TextUtils.isEmpty(headText)) {// The head is not empty
            initHeaderText(context, typedArray, headText);
        }

        // Initialize the tail icon
        initEndIcon(typedArray);

        // Initialize the intermediate content
        initContentText(context, typedArray);

        // Reclaim the resource object
        typedArray.recycle();
    }

    /** * Initializes the view **@paramContext Context */
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.common_textview_select, this);
        tvHead = findViewById(R.id.tv_text_select_header);
        tvContent = findViewById(R.id.tv_text_select_content);
        ivEndIcon = findViewById(R.id.tv_text_select_icon);
    }

    /** * Initializes header text **@paramContext *@paramTypedArray Resource object *@paramText Header content */
    private void initHeaderText(Context context, TypedArray typedArray, CharSequence text) {
        // Header font color
        setViewTextColor(context, tvHead, R.styleable.SelectTextView_headerTextColor, typedArray);
        // Header font size
        setViewTextSize(tvHead, R.styleable.SelectTextView_headerTextSize, typedArray);
        // Start spacing between heads
        int paddingStart = (int)typedArray.getDimension(R.styleable.SelectTextView_headerPaddingStart, 0);
        // End spacing of the head
        int paddingEnd = (int)typedArray.getDimension(R.styleable.SelectTextView_headerPaddingEnd, 0);
        // Top spacing of the head
        int paddingTop = (int)typedArray.getDimension(R.styleable.SelectTextView_headerPaddingTop, 0);
        // Spacing at the bottom of the head
        int paddingBottom = (int)typedArray.getDimension(R.styleable.SelectTextView_headerPaddingBottom, 0);

        tvHead.setText(text);
        tvHead.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
    }

    /** * Initializes the tail icon **@paramTypedArray Resource object */
    private void initEndIcon(TypedArray typedArray) {
        boolean flag = typedArray.getBoolean(R.styleable.SelectTextView_endIconVisible, true);
        if (flag) {
            ivEndIcon.setVisibility(VISIBLE);
            ivEndIcon.setImageResource(typedArray.getResourceId(R.styleable.SelectTextView_endIcon,
                    R.mipmap.common_arrow_right_gray));
        } else{ ivEndIcon.setVisibility(GONE); }}/** * Initializes the intermediate text **@paramContext *@paramTypedArray Resource object */
    private void initContentText(Context context, TypedArray typedArray) {
        // Initial content
        CharSequence text = typedArray.getText(R.styleable.SelectTextView_android_text);
        if(! TextUtils.isEmpty(text)) { tvContent.setText(text); }// Font size
        setViewTextSize(tvContent, R.styleable.SelectTextView_android_textSize, typedArray);
        // Font color
        setViewTextColor(context, tvContent, R.styleable.SelectTextView_android_textColor, typedArray);
        // Set the spacing
        setContentPadding(typedArray);
        // Set the margin
        setContentMargin(typedArray);
        // Set the mode for it
        setContentGravity(typedArray);
        // Set the prompt style
        initContentHint(context, typedArray);
    }

    /** * Sets the text spacing **@paramTypedArray Resource object */
    private void setContentPadding(TypedArray typedArray) {
        // Start spacing
        int paddingStart = (int)typedArray.getDimension(R.styleable.SelectTextView_textPaddingStart, 0);
        // End the spacing
        int paddingEnd = (int)typedArray.getDimension(R.styleable.SelectTextView_textPaddingEnd, 0);
        // Top spacing
        int paddingTop = (int)typedArray.getDimension(R.styleable.SelectTextView_textPaddingTop, 0);
        // Bottom spacing
        int paddingBottom = (int)typedArray.getDimension(R.styleable.SelectTextView_textPaddingBottom, 0);
        tvContent.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
    }

    /** * sets the text margin **@paramTypedArray Resource object */
    private void setContentMargin(TypedArray typedArray) {
        // Start margin
        int marginStart = (int)typedArray.getDimension(R.styleable.SelectTextView_textMarginStart, 0);
        // End margin
        int marginEnd = (int)typedArray.getDimension(R.styleable.SelectTextView_textMarginEnd, 0);
        // Top margin
        int marginTop = (int)typedArray.getDimension(R.styleable.SelectTextView_textMarginTop, 0);
        // Bottom margin
        int marginBottom = (int)typedArray.getDimension(R.styleable.SelectTextView_textMarginBottom, 0);
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)tvContent.getLayoutParams();
        layoutParams.setMargins(marginStart, marginTop, marginEnd, marginBottom);
        tvContent.setLayoutParams(layoutParams);
    }

    /** * sets its mode **@paramTypedArray Resource object */
    private void setContentGravity(TypedArray typedArray) {
        tvContent.setGravity(typedArray.getInt(R.styleable.SelectTextView_android_gravity,
                Gravity.END|Gravity.CENTER_VERTICAL));
    }

    /** * Initializes text prompt text **@paramContext on context *@paramTypedArray Resource object */
    private void initContentHint(Context context, TypedArray typedArray) {
        // Prompt text color
        int color = typedArray.getColor(R.styleable.SelectTextView_android_textColorHint,
                ContextCompat.getColor(context, R.color.c_D2D0DC));
        tvContent.setHintTextColor(color);
        CharSequence hintText = typedArray.getText(R.styleable.SelectTextView_android_hint);
        if(! TextUtils.isEmpty(hintText)) {// The prompt text is not empty
            // Prompt contenttvContent.setHint(hintText); }}/** * Sets the font size **@paramView is set to object *@paramAttrId Attribute Id *@paramTypedArray Resource object */
    private void setViewTextSize(TextView view, int attrId, TypedArray typedArray) {
        float size = typedArray.getDimension(attrId, 14 * view.getPaint().density);
        view.getPaint().setTextSize(size);
    }

    /** * Sets the font color **@paramContext *@paramView is set to object *@paramAttrId Attribute Id *@paramTypedArray Resource object */
    private void setViewTextColor(Context context, TextView view, int attrId, TypedArray typedArray) {
        int color = typedArray.getColor(attrId,
                ContextCompat.getColor(context, R.color.c_2B303C));
        view.setTextColor(color);
    }

    /** * get content **@returnContent * /
    public CharSequence getText(a) {
        if (tvContent == null) {
            return null;
        } else {
            returntvContent.getText(); }}/** * Set the content **@paramText Set content */
    public void setText(CharSequence text) {
        if(tvContent ! =null) { tvContent.setText(text); }}}Copy the code