android: layout_margin和layout_marginTop的冲突
一,代码:
如果同时在属性中同时设置了:
android:layout_margin="10dp"
android:layout_marginTop="30dp"
则只有layout_margin生效,
layout_marginTop无效,并不会覆盖layout_margin的值.
如果layout_margin放在style中,也是如此
android:padding="10dp"
android:paddingTop="10dp"
这两个属性也是如此
看一个例子:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.Flex1Activity">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_margin="20dp"
android:layout_marginTop="150dp"
android:text="你真的了解这样的世界吗"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_marginTop="150dp"
android:text="你真的了解王富贵吗"/>
<TextView
style="@style/TextStyle"
android:gravity="center"
android:layout_marginTop="150dp"
android:text="你真的了解你自己吗" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
二,测试效果:

可以看到第一个layout_marginTop没有生效,而是被layout_margin覆盖了
第二个layout_marginTop则有效,
浙公网安备 33010602011771号