代码之家  ›  专栏  ›  技术社区  ›  sir-haver

文本视图形状显示为椭圆形,即使形状为圆形

  •  0
  • sir-haver  · 技术社区  · 5 年前

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid
            android:color="#FFF" />
        <size
            android:width="5dp"
            android:height="5dp"/>
    
        <padding
            android:left="5dip"
            android:right="5dip"
            android:top="5dip"
            android:bottom="5dip" />
    </shape>
    

    在预览中,它看起来是圆形的,而不是椭圆形的。然后我在文本视图中实现了它:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent">
        <Button
            android:id="@+id/addToCartBtn"
            android:layout_width="match_parent"
            android:text="@string/cart_button_text"
            android:textColor="#FFF"
            android:background="@color/colorPrimary"
            android:layout_height="65dp"/>
        <TextView
            android:id="@+id/cartItemsCountTV"
            android:elevation="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginStart="16dp"
            android:layout_alignStart="@id/addToCartBtn"
            android:text="1"
            android:textSize="16sp"
            android:textStyle="bold"
            android:background="@drawable/circle"/>
    </RelativeLayout>
    

    结果仍然是椭圆形:

    enter image description here

    只有当文本为两位数字(如“10”)时,形状才会变为圆形。为什么它是椭圆形的,即使它应该是圆形的?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Md. Asaduzzaman    5 年前

    使用相同的 layout_width layout_height 把它保持成圆形而不是 wrap_content .

    <TextView
        ....
        android:layout_width="40dp"
        android:layout_height="40dp"
        ..../>
    

    包装内容 足够包含它包含的值(或子控件)。