[英]How to stop child Linearlayout to overlap parent Linearlayout rounded corners?
我有一個LinearLayout,它是父布局,它有圓角。 在這個父布局中有一個具有尖角的子 Linearlayout。 現在的問題是子 Linearlayout 與父 Linearlayout 的圓角重疊。 我知道我們也可以通過給孩子父母圓角來解決這個問題。 但我需要另一種方式。 我們該如何解決? 這是我嘗試過的代碼
<!-- HorizontalLinearStart --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="@drawable/whiteroundedbg"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:baselineAligned="false"
android:background="#ededed">
</LinearLayout>
</LinearLayout><!-- HorizontalLinearEnd -->
如果您使用 API 級別 21 及更高級別,則可以在代碼中執行此操作
outerLayout.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
outerLayout.setClipToOutline(true);
在您的子線性布局中,使用以下語法添加邊距:
android:layout_margin="24dp"
您應該用您自己的值替換邊距 dp 值,以便它給出所需的結果。
只需交換兩個線性布局的內容:-
<!-- HorizontalLinearStart -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
android:baselineAligned="false"
android:background="#ededed"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="@drawable/ic_launcher_background"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:elevation="4dp">
</LinearLayout>
注意:-保持線性布局的高度和寬度相同
方法二
或者為什么不使用視圖而不是視圖組
例子:
<androidx.cardview.widget.CardView android:clipToPadding="true"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="30dp"
android:layout_width="200dp"
android:layout_height="200dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"
android:id="@+id/customProfileGridImg"
android:layout_width="200dp"
android:layout_height="200dp"
android:paddingBottom="100dp"
android:paddingTop="100dp"/>
</androidx.cardview.widget.CardView>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.