我有一个textview,希望它的角是圆形的。我已经知道它可以使用android:background="@drawable/somefile"。在我的情况下,这个标签已经包括,所以不能再次使用。例如android:background="@drawable/mydialogbox"已经在后台创建图像

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:background="@drawable/mydialogbox"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textview_name"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    </LinearLayout>

</RelativeLayout>

所以当我想textview(textview_name)也与圆角,这是如何实现的。


当前回答

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dp" />
            <solid android:color="#ffffff"/>

        </shape>
    </item>
</layer-list>

其他回答

除了半径,还有一些圆角属性,比如topRightRadius, topLeftRadius, bottomRightRadius, bottomLeftRadius

示例TextView与红色边界角和灰色背景

Bg_rounded.xml(在drawables文件夹中)

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="10dp"
        android:color="#f00" />

    <solid android:color="#aaa" />

    <corners
        android:radius="5dp"
        android:topRightRadius="100dp" />
</shape>

TextView

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_rounded"
    android:text="Text"
    android:padding="20dp"
    android:layout_margin="10dp"
    />

结果

在drawable文件夹下创建一个XML gradient.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle"  >
            <corners android:radius="50dip" />
            <stroke android:width="1dip" android:color="#667162" />
            <gradient android:angle="-90" android:startColor="#ffffff" android:endColor="#ffffff" />
        </shape>
    </item>
</selector>

然后将此添加到您的TextView

android:background="@drawable/gradient"

你可以使用提供的矩形形状(没有渐变,除非你想要一个)如下所示:

在可拉的/ rounded_rectangle.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <stroke android:width="1dp" android:color="#ff0000" />
    <solid android:color="#00ff00" />
</shape>

然后在文本视图中:

android:background="@drawable/rounded_rectangle"

当然,您需要自定义尺寸和颜色。

你可以使用SVG圆角和加载到一个ImageView和使用ConstraintLayout带来ImageView的TextView

我用它圆润ImageView和圆润TextView

有两个步骤

1)在你的可绘制文件夹中创建这个文件:- rounded_corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
         <corners android:radius="10dp" />  // set radius of corner
         <stroke android:width="2dp" android:color="#ff3478" /> // set color and width of border
         <solid android:color="#FFFFFF" /> // inner bgcolor
</shape>

2)设置这个文件到你的TextView作为背景属性。

android:background="@drawable/rounded_corner"

你也可以在Button或Edittext中使用这个drawable