android studio xml控件垂直居中
时间: 2023-09-11 12:11:28 浏览: 130
在Android Studio中,可以通过以下方式将XML布局中的控件垂直居中:
1. 使用LinearLayout:将控件放在LinearLayout中,设置android:gravity="center_vertical"属性即可。
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
```
2. 使用RelativeLayout:将控件放在RelativeLayout中,设置android:layout_centerVertical="true"属性即可。
```
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerVertical="true"/>
</RelativeLayout>
```
3. 使用ConstraintLayout:将控件放在ConstraintLayout中,设置app:layout_constraintVertical_bias="0.5"属性即可。
```
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
阅读全文
相关推荐

















