欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

安卓----文字下方加横线

程序员文章站 2022-11-05 10:03:32
public class WhiteTextviewWithWhiteBottomLine extends LinearLayout { private Context mContext; public WhiteTextviewWithWhiteBottomLine(Context context) { this(context, null); } public WhiteTextviewWithWhiteBottomLine(Context con....
public class WhiteTextviewWithWhiteBottomLine extends LinearLayout {

    private Context mContext;

    public WhiteTextviewWithWhiteBottomLine(Context context) {
        this(context, null);
    }

    public WhiteTextviewWithWhiteBottomLine(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WhiteTextviewWithWhiteBottomLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initViews();
    }

    private TextView textView;
    private View viewLine;

    private void initViews() {
        View view = View.inflate(mContext, R.layout.view_textview_with_bottom_line, null);

        textView = view.findViewById(R.id.tv_view_textview_with_line);
        viewLine = view.findViewById(R.id.view_view_textview_with_line);

        this.addView(view);

        setSelected(false);
    }

    public void setTextViewText(String s) {
        textView.setText(s);
    }

    public void setLineColor(@ColorInt int color) {
        viewLine.setBackgroundColor(color);
    }

    public void setSelected(boolean isSelected) {

        if (isSelected) {
            viewLine.setVisibility(VISIBLE);
        } else {
            viewLine.setVisibility(GONE);
        }
    }

    public boolean isSelected() {
        if (viewLine.getVisibility() == View.VISIBLE) {
            return true;
        }else {
            return false;
        }
    }
}

布局文件:

<?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">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_view_textview_with_line"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="测试"
            android:textColor="@color/white"
            android:textSize="12sp" />

        <View
            android:id="@+id/view_view_textview_with_line"
            android:layout_width="30dp"
            android:layout_height="1dp"
            android:layout_gravity="center_horizontal"
            android:background="@color/white" />

    </LinearLayout>

</LinearLayout>

如果想改变文字、线的颜色这些,直接修改布局文件中的内容。

本文地址:https://blog.csdn.net/Jermmy1207/article/details/107482811