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


当前回答

把你的TextView内MaterialCardView:

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="25dp"
        >
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/emergency_sms_template"
            android:padding="6dp"
            android:textSize="11sp"
            />
    </com.google.android.material.card.MaterialCardView>

其他回答

右键单击可绘制文件夹并创建新文件 根据您的要求命名文件,并将扩展名添加为.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"

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

我用它圆润ImageView和圆润TextView

在材质组件库中,你可以使用MaterialShapeDrawable。

使用TextView:

    <TextView
        android:id="@+id/textview"
        ../>

你可以通过编程方式应用MaterialShapeDrawable:

float radius = getResources().getDimension(R.dimen.corner_radius);

TextView textView = findViewById(R.id.textview);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED,radius)
        .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(textView,shapeDrawable);

如果你想改变背景颜色和边框,只需应用:

shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.....));
shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color....));
<?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>

简单地使用圆角图像作为该视图的背景

别忘了把你的自定义图像放在可绘制文件夹中

android:background="@drawable/my_custom_image"