在Android中绘制圆角矩形

时间:2020-12-25 18:34:11

标签: android kotlin android-drawable shapes

我找到了 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>

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

编辑 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

1 个答案:

答案 0 :(得分:3)

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

这里是 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>

enter image description here

更新

<块引用>

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

您不能像 <shape>layer-list 那样直接在 xml 布局中使用 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>

enter image description here

相关问题