Enz*_*zio 2 android android-layout
我知道 layout_gravity = "center" 会将当前视图或布局置于其父布局中。
在我给出的代码示例中,当它们的父项是 ConstraintLayout 时,第一个 TextView 和 LinearLayout 都不能将 layout_gravity 作为属性。
但是,当第二个 TextView 和第二个 LinearLayout 的父级是 LinearLayout 时,它们可以具有 layout_gravity 属性。
为什么呢?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="heey"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"></LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
ConstraintLayout
不支持layout_gravity
。你应该使用
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Run Code Online (Sandbox Code Playgroud)
使布局居中。
归档时间: |
|
查看次数: |
4545 次 |
最近记录: |