[英]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.