【问题标题】:text view shape is shown as oval even though the shape is cirlce即使形状是圆形,文本视图形状也显示为椭圆形
【发布时间】:2020-02-09 07:49:10
【问题描述】:

我创建了一个名为 circle.xml 的圆形:

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

那么结果还是椭圆形:

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

【问题讨论】:

    标签: android xml drawable


    【解决方案1】:

    使用相同的layout_widthlayout_height 将其保持为圆形,而不是wrap_content

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

    使用视图的大小为wrap_content 将迫使它仅扩展很远 足以包含它所包含的值(或子控件)。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多