To draw rounded rectangle in Android

寵の児 提交于 2021-01-04 04:19:10

问题


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>

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

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)

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 layout :

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

SOLUTION :

here


回答1:


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

Here is 120dp radii

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

UPDATE

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.

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>



来源:https://stackoverflow.com/questions/65450741/to-draw-rounded-rectangle-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!