Issue
I'm trying to build a layout with a ConstraintLayout with rounded corners inside a ScrollView. At the top of the layout I want to place an image that should have the top corners rounded (to match the layout corners) and the bottom corners straight.
Following the suggestions I found in this StackOverflow question here's my code:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ScrollView
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="7.5"
android:background="@color/darkModePrimary">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:background="@drawable/layout_bg"
android:layout_height="wrap_content">
<ImageView
android:background="@drawable/layout_bg"
android:src="@drawable/house_medium"
android:id="@+id/house_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_marginTop="32dp"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/house_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some text"
android:textSize="24sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
This is the xml file where I define the shape for the background of the ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white"/>
<corners android:radius="10dp"/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
If I set this shape as the background of both the Constraint Layout and the ImageView, I get - as expected - an image with all four rounded corners, as shown here.
I've tried to create another shape to use as background of the image, like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white"/>
<corners android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>
</shape>
But when I set it as background of the ImageView I don't get what I want, all four corners of the image are now straight
What am I doing wrong?
Solution
To make it working just set outline to your card container layout in code.
In layout xml add id to card container:
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/card" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginTop="16dp" android:layout_marginRight="16dp" android:background="@drawable/layout_bg" android:elevation="4dp">
In your MainActivity onCreate:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) card.clipToOutline = true card.outlineProvider = ViewOutlineProvider.BACKGROUND }
Answered By - dmtr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.