Aom*_*Set 10 android android-layout
是否可以创建像div这样的容器?问题是我有4个控件(TextView,Buttons和EditView),我想把它们放在中心位置.但我不是一个接一个地做这些,而是想知道是否有一种聪明的方法可以做到这一点.在Web应用程序中,使用css,我们可以在div上执行此操作:
margin-left: auto;
margin-right: auto;
height: xxx;
widht: yyy;
Run Code Online (Sandbox Code Playgroud)
然后div将以页面为中心
在创建Android应用程序时是否有类似的方法?
在XML中,等同于a Div,是a ViewGroup.
例如:LinearLayout,RelativeLayout等
ViewGroups可以包含视图和控件,如:TextView,Button和EditView.
我创建了一个我认为你问的例子:
我用a LinearLayout来保存我们的视图和控件.
然后我用android:gravity="center_horizontal"我们的LinearLayout方式让孩子们居中.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity" >
<TextView
android:layout_width="200dp"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是它的样子:

android:layout_gravity="center|center_vertical|center_horizontal|right|left"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15241 次 |
| 最近记录: |