-1

Hello Android ブックでこのコードを読みましたが、2 つの LinearLayout を記述する理由がわかりません。私はこれらのうちの 1 人になることができますか? ネストされた LinearLayout を使用する理由 それは何をするためのものか?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="30dip"
    android:orientation="horizontal">
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center">
        <TextView
            android:text="@string/main_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="25dip"
            android:textSize="24.5sp"/>
        <Button
            android:id="@+id/continue_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/continue_label"/>
        <Button
            android:id="@+id/new_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/new_game_label"/>
        <Button
            android:id="@+id/about_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/about_label" />
        <Button 
            android:id="@+id/exit_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/exit_label"/>
   </LinearLayout>
</LinearLayout>

私を助けてください。ありがとうございました。

4

2 に答える 2

4

水平レイアウトは、次のように要素を水平にレイアウトします。

[element][element][element]

垂直のものは、次のようにレイアウトしています。

[element]
[element]
[element]

表示されている xml 形式では、縦に並んだボタンの行になります。そのレイアウトが必要ない場合は、1 つ削除して単純化できます。

文字で説明するのは難しいですが、これは次のようなものです。

----Outer (Horizontal) layout-----
|                                |
|  ---Inner (Vertical) layout-   |
|  |       [Textview]        |   |
|  |       [Button]          |   |
|  |       [Button]          |   |
|  |       [Button]          |   |
|  ---------------------------   |
----------------------------------

テキストビュー/ボタンが垂直に下に移動します。

編集:

実際には外側のレイアウトが

android:layout_width="fill_parent"
android:layout_height="fill_parent"

画面全体を占めるだけで、パディングは 30 です。次に、内側のレイアウトがその中のコンテンツに垂直方向に収まり、幅が水平方向に最大になります。

おそらく外側のレイアウトは必要ないと思います..適切な属性を1つだけ使用して同じ効果を得ることができますLinearLayout

于 2012-09-01T05:13:44.790 に答える
0

ここでは、レイアウトの方向を維持するために使用される 2 つの LinearLayout を示します。

最初または親 LinearLayout は、画面全体をカバーすることができます。

android:layout_width="fill_parent"
android:layout_height="fill_parent"

とともに

背景色

そしてキューに水平にすべてを追加し、続いて次の理由で

android:orientation="vertical"[![enter image description here][1]][1]

2番目のLinearLayoutまたはネストされたものは、すべてを中央に配置します

android:layout_gravity="center"

アイテムが必要とするだけのスペースを取ります

android:layout_height="wrap_content"
android:layout_width="fill_parent"

すべてが垂直方向に配置されます。

android:orientation="vertical"
于 2017-04-12T09:53:40.700 に答える