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,它定义其方向是垂直的 – 所有子级 View(其中有两个)都将在垂直方向堆叠。 第一个子级是另一个 LinearLayout,它使用水平方向,第二个子级是一个 LinearLayout,它使用垂直方向。 每个嵌套的 LinearLayout 都包含多个 TextView 元素,这些元素按它们的父对象 LinearLayout 所定义的方式彼此定向。

现在打开 HelloLinearLayout.cs,并确保它在OnCreate() 方法中加载 Resources/Layout/Main.axml 布局:

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

SetContentView(int)) 方法加载 Activity 的布局文件(由资源 ID 指定)– Resources.Layout.Main 指 Resources/Layout/Main.axml 布局文件。

运行该应用程序。 应该看到以下内容:

Screenshot of app first LinearLayout arranged horizontally, second vertically

请注意 XML 属性如何定义每个视图的行为。 尝试为 android:layout_weight 使用不同的值来进行试验,了解屏幕实际使用面积如何根据每个元素的权重进行分布。 请参阅常用布局对象来详细了解 LinearLayout 如何处理 android:layout_weight 属性。

参考

本页的部分内容是根据 Android 开放源代码项目创建和共享的工作进行的修改,并根据知识共享署名 2.5 通用许可协议中的条款进行使用。