andriod studio将控件居中
时间: 2023-07-29 13:07:33 浏览: 87
要将控件居中,可以在布局文件中使用以下两种方法:
1. 使用RelativeLayout布局,将控件的属性设置为android:layout_centerInParent="true",如下所示:
```
<RelativeLayout 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"
android:layout_centerInParent="true"
android:text="Hello World!" />
</RelativeLayout>
```
2. 使用LinearLayout布局,将控件的属性设置为android:layout_gravity="center",如下所示:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!" />
</LinearLayout>
```
阅读全文
相关推荐
















