是否可以创建像 div
这样的容器?
问题是我有 4 个控件(TextView
、Buttons
和 EditView
),我想将它们居中。但是我没有一个一个地做,而是想知道是否有一种聪明的方法可以做到这一点。在 Web 应用程序中,使用 css
,我们可以在 div
上执行此操作:
margin-left: auto;
margin-right: auto;
height: xxx;
width: yyy;
然后 div
将在页面上居中
在创建 Android 应用程序时是否有类似的方法?
最佳答案
在 XML 中,Div
相当于 ViewGroup
。
例如:LinearLayout
、RelativeLayout
等
ViewGroups 可以包含 View 和控件,例如:TextView
、Button
和 EditView
。
我创建了一个我认为您要问的示例:
我使用 LinearLayout
来保存我们的 View 和控件。
然后我在我们的 LinearLayout
上使用 android:gravity="center_horizontal"
来让 child 居中。
<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>
这是它的样子:
关于android - 容器,如 div、span 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18762096/