在Android中绘制圆角矩形 [英] To draw rounded rectangle in Android

查看:455
本文介绍了在Android中绘制圆角矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个问题,解决方案是此代码:

I have found this question and the solution was this code :

<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上不起作用(无圆角).你呢 ?有变化吗?

This code doesn't work on my PC (no rounded corners). And you ? There has been changes ?

构建项目时出现错误:AAPT:错误:XML或文本声明不在实体开始时. (最终纠正:我的愚蠢错误)

EDIT 1: I have an error when I build the project : AAPT: error: XML or text declaration not at start of entity. (finally corrected : stupid mistake from me)

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

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.

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>

解决方案:

此处

推荐答案

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

I think you couldn't see the rounded rectangle as the radii are small so that they might not be noticed, try to set larger values for the four corners

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

更新

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

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布局,而是可以使用具有某些布局视图属性(例如android:background)的可绘制资源,如下所示:

You can't use drawable tags into xml layout directly like <shape> or layer-list, instead you can refer to drawable resource with some layout view attributes like android:background as below

<?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>

这篇关于在Android中绘制圆角矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆