为什么我在Android Studio 3.1.1 中使用constraint 时两个TextView 位于LinearLayout 的中心?
Why are two TextView located the center of LinearLayout when I use constraint in Android Studio 3.1.1?
我希望设计UI,UI的顶部是AD,UI的底部是一个关闭按钮,名为myLinearLayout的LinearLayout会填充其他space。
所以我写了下面的代码,然后我在myLinearLayout中添加了两个TextView控件tvDetail和tvName。
我没有为这两个 TextView 控件添加任何约束,我认为这两个 TextView 控件将位于 myLinearLayout 的左侧和顶部。
但实际上它们位于 myLinearLayout 的中心(请看图片),为什么?
代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_unit_id"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/adView"
app:layout_constraintBottom_toTopOf="@id/btnClose"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<TextView
android:id="@+id/tvDetail"
style="@style/myTextMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details"
/>
<TextView
android:id="@+id/tvName"
style="@style/myTextMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
/>
</LinearLayout>
<Button
android:id="@+id/btnClose"
style="@style/myTextMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="@string/BtnClose"
/>
</android.support.constraint.ConstraintLayout>
图片
您的 LinearLayout
有 wrap_content
宽度和高度。
将它们设置为 0dp
:
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
...>
0dp 相当于 "MATCH_CONSTRAINT"。参见 ConstraintLayout documentation。
这是因为您的 LinearLayout
上有以下内容
android:layout_width="wrap_content"
android:layout_height="wrap_content"
此 'shrinks' LinearLayout 的宽度和高度仅与其包含的内容一样。
按如下方式更改您的 layout_width
和 layout_height
:
android:layout_width="match_parent"
android:layout_height="match_parent"
或者,给 layout_height
和 layout_width
一定的大小。
我希望设计UI,UI的顶部是AD,UI的底部是一个关闭按钮,名为myLinearLayout的LinearLayout会填充其他space。 所以我写了下面的代码,然后我在myLinearLayout中添加了两个TextView控件tvDetail和tvName。
我没有为这两个 TextView 控件添加任何约束,我认为这两个 TextView 控件将位于 myLinearLayout 的左侧和顶部。
但实际上它们位于 myLinearLayout 的中心(请看图片),为什么?
代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_unit_id"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/adView"
app:layout_constraintBottom_toTopOf="@id/btnClose"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<TextView
android:id="@+id/tvDetail"
style="@style/myTextMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details"
/>
<TextView
android:id="@+id/tvName"
style="@style/myTextMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
/>
</LinearLayout>
<Button
android:id="@+id/btnClose"
style="@style/myTextMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="@string/BtnClose"
/>
</android.support.constraint.ConstraintLayout>
图片
您的 LinearLayout
有 wrap_content
宽度和高度。
将它们设置为 0dp
:
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
...>
0dp 相当于 "MATCH_CONSTRAINT"。参见 ConstraintLayout documentation。
这是因为您的 LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
此 'shrinks' LinearLayout 的宽度和高度仅与其包含的内容一样。
按如下方式更改您的 layout_width
和 layout_height
:
android:layout_width="match_parent"
android:layout_height="match_parent"
或者,给 layout_height
和 layout_width
一定的大小。