实现圆角约束布局
Implementing rounded constraint layout
我有一个带圆角的 ConstraintLayout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="@drawable/backgrond_drawable"
app:layout_constraintWidth_percent="0.3"
app:layout_constraintDimensionRatio="1:1.5"
>
</androidx.constraintlayout.widget.ConstraintLayout>
这是 backgrond_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:radius="20dip" />
<stroke android:width="1dip" android:color="#222222" />
<gradient android:angle="-90" android:startColor="#222222" android:endColor="#222222" />
</shape>
</item>
当我将 TextView 添加到 ConstraintLayout 的底部时,它消除了圆角:
我该怎么办?
你需要有第二个 drawable 来使 textview 的下角变圆
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"/>
<stroke android:width="1dip" android:color="#222222" />
<gradient android:angle="-90" android:startColor="#222222" android:endColor="#222222" />
</shape>
</item>
并将此可绘制对象设置为 textView 的背景。
或者您可以使用卡片布局,然后使用文本视图
例如
<CardView.
.
.
.
cardCornerRadius:"20dp"
>
<ContraintLayout>
<TextView>
</TextView>
</ContraintLayout>
</CardView>
将您的 TextView 背景设置为透明,这将解决您的问题;我希望你可以尝试下面的方法。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="100dp"
android:background="@drawable/backgrond_drawable"
app:layout_constraintDimensionRatio="1:1.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="center"
android:text="Testing text"
android:textColor="@color/white"
app:backgroundTint="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我有一个带圆角的 ConstraintLayout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="@drawable/backgrond_drawable"
app:layout_constraintWidth_percent="0.3"
app:layout_constraintDimensionRatio="1:1.5"
>
</androidx.constraintlayout.widget.ConstraintLayout>
这是 backgrond_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:radius="20dip" />
<stroke android:width="1dip" android:color="#222222" />
<gradient android:angle="-90" android:startColor="#222222" android:endColor="#222222" />
</shape>
</item>
当我将 TextView 添加到 ConstraintLayout 的底部时,它消除了圆角:
我该怎么办?
你需要有第二个 drawable 来使 textview 的下角变圆
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"/>
<stroke android:width="1dip" android:color="#222222" />
<gradient android:angle="-90" android:startColor="#222222" android:endColor="#222222" />
</shape>
</item>
并将此可绘制对象设置为 textView 的背景。
或者您可以使用卡片布局,然后使用文本视图
例如
<CardView.
.
.
.
cardCornerRadius:"20dp"
>
<ContraintLayout>
<TextView>
</TextView>
</ContraintLayout>
</CardView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="100dp"
android:background="@drawable/backgrond_drawable"
app:layout_constraintDimensionRatio="1:1.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="center"
android:text="Testing text"
android:textColor="@color/white"
app:backgroundTint="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>