在 Android 中绘制圆角矩形

To draw rounded rectangle in Android

我找到了 this question,解决方案是这段代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

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

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

此代码在我的电脑上不起作用(没有圆角)。和你 ?有变化吗?

编辑 1: 我在构建项目时遇到错误:AAPT:错误:XML 或文本声明不在实体的开头。 (终于更正了:我的愚蠢错误)

编辑 2: 现在的错误是: 找不到以下 类: - 角落(修复构建路径,编辑 XML) - 形状(修复构建路径,编辑 XML) - 实体(修复构建路径,编辑 XML) 提示:尝试构建项目。

XML布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <shape
        android:shape="rectangle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

        <corners
            android:bottomLeftRadius="120dp"
            android:bottomRightRadius="120dp"
            android:topLeftRadius="120dp"
            android:topRightRadius="120dp" />

    </shape>
</androidx.constraintlayout.widget.ConstraintLayout>

解决方案:

here

我认为你看不到圆角矩形是因为半径太小所以可能不会被注意到,请尝试为四个角设置更大的值

这里是120dp半径

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

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

    <corners
        android:bottomLeftRadius="120dp"
        android:bottomRightRadius="120dp"
        android:topLeftRadius="120dp"
        android:topRightRadius="120dp" />

</shape>

更新

EDIT 2 : The error is now : The following classes could not be found: - corners (Fix Build Path, Edit XML) - shape (Fix Build Path, Edit XML) - solid (Fix Build Path, Edit XML) Tip: Try to build the project.

您不能像 <shape>layer-list 那样直接在 xml 布局中使用 drawable 标签,而是可以使用一些布局视图属性来引用 drawable 资源,例如 android:background 如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/test"
        android:padding="8dp"
        android:text="Hello World!"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="22sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>