如何将文本置于自定义" square"的TextView

时间:2018-02-09 20:58:43

标签: android

我已创建自定义TextView,因为我想:

  • 其高度以匹配某些约束(从另一个TextView的顶部到顶部和底部到底部)
  • 其宽度与该高度相匹配,因此设置为TextView背景的椭圆形xml显示一个圆圈。

这是自定义视图的代码:

public class SquareTextView extends AppCompatTextView {
    public static final String TAG = "SquareTextView";
    int squareDim = 0;

    public SquareTextView(Context context) {
        super(context);
    }

    public SquareTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int h = this.getMeasuredHeight();
        int w = this.getMeasuredWidth();
        squareDim = Math.max(w, h);

        setMeasuredDimension(squareDim, squareDim);
    }
}

layout xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.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="wrap_content">

        <TextView
            android:id="@+id/tv_registration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            android:text="WW-WWW"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_aircraft_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MMMMMMMMM"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            app:layout_constraintStart_toStartOf="@+id/tv_registration"
            app:layout_constraintTop_toBottomOf="@+id/tv_registration" />

        <com.mydomain.SquareTextView
            android:id="@+id/tv_counter"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginEnd="24dp"
            android:background="@drawable/shape_circle"
            android:gravity="center"
            android:text="0"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/tv_registration"
            app:layout_constraintBottom_toTopOf="@+id/cv_ops_restrictions"/>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

shape_circle.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <size android:width="50dp"
        android:height="50dp"/>
    <solid android:color="@android:color/white"/>
</shape>

问题是我希望文本以圆圈为中心。关于如何实现这个目标或者我做错了什么的指示?目前它看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:3)

我建议您不要在这里使用自定义TextView子类。您已经在ConstraintLayout内工作,它支持其子级的宽高比。这意味着您可以将<com.mydomain.SquareTextView>替换为<TextView>并添加此属性以获得完美的方形视图:

app:layout_constraintDimensionRatio="1:1"