【问题标题】:xml - unable to set shape as background of a TextViewxml - 无法将形状设置为 TextView 的背景
【发布时间】:2018-04-19 16:13:35
【问题描述】:

我正在尝试在android中设置一个带有笔划的透明圆圈作为TextView的背景:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:background="@drawable/decibel_circle"
        android:text="30 dB"
        android:fontFamily="sans-serif"
        android:textColor="@android:color/black"
        android:textSize="70dp" />
</RelativeLayout>

这是我要使用的形状,位于 decibel-circle.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    <stroke
        android:width="5dp"
        android:color="#F44336">
    </stroke>
    <solid android:color="@android:color/transparent"/>
    <size
        android:width="36dp"
        android:height="36dp" />
    <corners android:radius="12dp" />
    </shape>
</selector>

问题是形状从未显示,无论是在预览中还是在我运行我的应用程序时。 我做错了什么?

【问题讨论】:

    标签: android xml android-shape


    【解决方案1】:

    在您的 decibel-circle.xml 中删除 selector 只需单独使用 shape

    只用这个

     <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    <stroke
        android:width="5dp"
        android:color="#F44336">
    </stroke>
    <solid android:color="@android:color/transparent"/>
    <size
        android:width="36dp"
        android:height="36dp" />
    <corners android:radius="12dp" />
    </shape>
    

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 2012-03-05
      • 1970-01-01
      • 2014-04-21
      • 2021-04-08
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多