我有一个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)也与圆角,这是如何实现的。


当前回答

除了半径,还有一些圆角属性,比如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"
    />

结果

其他回答

Create rounded_corner.xml in the drawable folder and add the following content, <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="1dp" android:color="@color/common_border_color" /> <solid android:color="#ffffff" /> <padding android:left="1dp" android:right="1dp" android:bottom="1dp" android:top="1dp" /> <corners android:radius="5dp" /> </shape> Set this drawable in the TextView background property like so: android:background="@drawable/rounded_corner"

我希望这对你有用。

除了半径,还有一些圆角属性,比如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"
    />

结果

有两个步骤

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

右键单击可绘制文件夹并创建新文件 根据您的要求命名文件,并将扩展名添加为.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"  />
      <solid android:color="#1e90ff" />
  </shape>

添加你想要的圆角边android:background="@drawable/corner"

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

在可拉的/ 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"

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