11

のようなコンテナを作成することは可能divですか? 問題は、4 つのコントロール ( TextViewButtonsおよびEditView) があり、これらを中央に配置したいことです。しかし、それらを1つずつ行うのではなく、これを行うスマートな方法があるかどうか疑問に思っていました. Web アプリケーションでは、 を使用して、以下cssでこれを行うことができますdiv

margin-left: auto;
margin-right: auto;
height: xxx;
width: yyy;

次に、divページの中央に配置されます

Androidアプリを作成するときに同様の方法はありますか?

4

2 に答える 2

8

XML では、aDivに相当するものは aViewGroupです。

例:LinearLayoutなどRelativeLayout

ViewGroups にはTextView、 、 、Buttonなどのビューとコントロールを含めることができますEditView

その他の ViewGroup サブクラス

私はあなたが求めていると思うものの例を作成しました:

LinearLayoutビューとコントロールを保持するために a を使用しました。

次に、 で を使用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>

外観は次のとおりです。

デモ

于 2013-09-12T16:40:07.900 に答える
1
android:layout_gravity="center|center_vertical|center_horizontal|right|left"
于 2013-09-12T11:01:36.207 に答える