【问题标题】:To draw rounded rectangle in Android在 Android 中绘制圆角矩形
【发布时间】:2020-12-25 18:34:11
【问题描述】:

我找到了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: error: XML or text declaration not at the start of entity。 (终于更正了:我犯的愚蠢错误)

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

【问题讨论】:

  • 请分享xml布局

标签: android kotlin android-drawable shapes


【解决方案1】:

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

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

更新

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

您不能像&lt;shape&gt;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>

【讨论】:

  • 谢谢。我已经尝试过这个并且不起作用。但是当我构建项目时出现错误:AAPT: error: XML or text declaration not at start of entity.
  • @jujuf1 可能有多个reason,但我不认为提供的drawable 可以用它做些什么.. 你能提供你的xml,如果你有一些java/kotlin 行为分享
  • 我在 xml 布局中有一个错误。错误是:找不到以下类:-角(修复构建路径,编辑 XML)-形状(修复构建路径,编辑 XML)-实体(修复构建路径,编辑 XML)提示:尝试构建项目。
  • 感谢您的回复,但如果您参考我发布的 stackoverflow 问题链接,开发人员设法做到这一点。我不明白
猜你喜欢
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 2015-03-22
  • 1970-01-01
相关资源
最近更新 更多