textview.xml
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textview_badge"
android:layout_width="0dp"
android:minWidth="@dimen/baseline_grid_3x"
android:layout_height="wrap_content" android:gravity="center"
android:layout_marginEnd="@dimen/item_padding"
android:background="@drawable/red_circle"
android:layout_marginBottom="@dimen/baseline_grid_2x"
android:layout_marginTop="@dimen/baseline_grid_2x"
android:padding="@dimen/baseline_grid_0.5x" tools:text="9"
android:textColor="@color/colorWhite"
android:textSize="@dimen/app_text_size_small"
android:textStyle="bold"/>
red_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
这是我的代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorRed" />
<stroke
android:width="0dp"
android:color="@color/colorRed" />
<size
android:width="20dp"
android:height="20dp"" />
</shape>
我正在尝试使用带文本的圆形显示textview当设备字体大小较小时设备字体大小较小然后textview背景变为圆形到椭圆形时我尝试将大小20dp更改为60dp然后变成圆形时,它正常工作请建议我如何修复它当我们增加字体大小时如何保持textview的圆形背景
答案 0 :(得分:2)
您可以使用ConstraintLayout
使TextView
宽度始终等于TextView
height =&gt;它会使TextView
背景始终为圆圈
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@drawable/red_circle"
android:gravity="center"
android:text="AAAAAAAAAAAA"
android:textSize="20sp"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>