Xamarin.Android LinearLayout

LinearLayout は ですViewGroup 子を表示するView 要素を垂直方向または水平方向に直線方向に配置します。

を過剰に使用する場合は注意が LinearLayout必要です。 複数 LinearLayoutの を入れ子にし始める場合は、RelativeLayout その代わりに。

HelloLinearLayout という名前の新しいプロジェクトを開始します。

Resources/Layout/Main.axml を開き、次を挿入します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation=    "vertical"
    android:layout_width=    "match_parent"
    android:layout_height=    "match_parent"    >

  <LinearLayout
      android:orientation=    "horizontal"
      android:layout_width=    "match_parent"
      android:layout_height=    "match_parent"
      android:layout_weight=    "1"    >
      <TextView
          android:text=    "red"
          android:gravity=    "center_horizontal"
          android:background=    "#aa0000"
          android:layout_width=    "wrap_content"
          android:layout_height=    "match_parent"
          android:layout_weight=    "1"    />
      <TextView
          android:text=    "green"
          android:gravity=    "center_horizontal"
          android:background=    "#00aa00"
          android:layout_width=    "wrap_content"
          android:layout_height=    "match_parent"
          android:layout_weight=    "1"    />
      <TextView
          android:text=    "blue"
          android:gravity=    "center_horizontal"
          android:background=    "#0000aa"
          android:layout_width=    "wrap_content"
          android:layout_height=    "match_parent"
          android:layout_weight=    "1"    />
      <TextView
          android:text=    "yellow"
          android:gravity=    "center_horizontal"
          android:background=    "#aaaa00"
          android:layout_width=    "wrap_content"
          android:layout_height=    "match_parent"
          android:layout_weight=    "1"    />
  </LinearLayout>
        
  <LinearLayout
    android:orientation=    "vertical"
    android:layout_width=    "match_parent"
    android:layout_height=    "match_parent"
    android:layout_weight=    "1"    >
    <TextView
        android:text=    "row one"
        android:textSize=    "15pt"
        android:layout_width=    "match_parent"
        android:layout_height=    "wrap_content"
        android:layout_weight=    "1"    />
    <TextView
        android:text=    "row two"
        android:textSize=    "15pt"
        android:layout_width=    "match_parent"
        android:layout_height=    "wrap_content"
        android:layout_weight=    "1"    />
    <TextView
        android:text=    "row three"
        android:textSize=    "15pt"
        android:layout_width=    "match_parent"
        android:layout_height=    "wrap_content"
        android:layout_weight=    "1"    />
    <TextView
        android:text=    "row four"
        android:textSize=    "15pt"
        android:layout_width=    "match_parent"
        android:layout_height=    "wrap_content"
       android:layout_weight=    "1"    />
  </LinearLayout>

</LinearLayout>

この XML を慎重に調べます。 ルートがあるLinearLayout 垂直方向の向きを定義する 。 (そのうちの 2 つを持つ) すべての子 Viewは垂直方向に積み上げされます。 最初の子は別の子ですLinearLayout 水平方向を使用し、2 番目の子は です。LinearLayout 垂直方向を使用する。 これらの入れ子になった LinearLayout各 s には、複数のTextView 要素。親 LinearLayoutによって定義された方法で相互に向けられています。

次に HelloLinearLayout.cs を開き、 の Resources/Layout/Main.axml レイアウトが読み込まれていることを確認しますOnCreate() メソッド:

protected override void OnCreate (Bundle savedInstanceState)
{
    base.OnCreate (savedInstanceState);
    SetContentView (Resource.Layout.Main);
}

) メソッドはSetContentView(int)、リソース ID Resources.Layout.Main で指定された のActivityレイアウト ファイルを読み込み、Resources/Layout/Main.axml レイアウト ファイルを参照します。

アプリケーションを実行します。 次のように表示されます。

アプリの最初の LinearLayout が水平方向に配置され、2 つ目が垂直方向に配置されているスクリーンショット

XML 属性によって各ビューの動作がどのように定義されるかに注目してください。 のさまざまな値 android:layout_weight を試して、各要素の重さに基づいて画面の不動産がどのように分散されるかを確認してください。 方法の詳細については、 共通レイアウト オブジェクト に関するドキュメントを参照してください。LinearLayout は 属性を処理します android:layout_weight

リファレンス

このページの一部は、Android オープン ソース プロジェクトによって作成および共有され、 クリエイティブ コモンズ 2.5 属性ライセンスに記載されている条件に従って使用される作業に基づく変更です。