Xamarin LinearLayoutXamarin.Android LinearLayout

LinearLayoutViewGroupLinearLayout is a ViewGroup 显示子Viewthat displays child View 垂直或水平的线性方向的元素。elements in a linear direction, either vertically or horizontally.

你应谨慎使用LinearLayoutYou should be careful about over-using the LinearLayout. 如果开始嵌套多个LinearLayout,可能需要考虑使用RelativeLayoutIf you begin nesting multiple LinearLayouts, you may want to consider using a RelativeLayout 是.instead.

启动名为HelloLinearLayout的新项目。Start a new project named HelloLinearLayout.

打开Resources/Layout/main.axml并插入以下内容:Open Resources/Layout/Main.axml and insert the following:

<?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。Carefully inspect this XML. 存在根LinearLayoutThere is a root LinearLayout 它将其方向定义为垂直 – 所有子View(其中有两个)将垂直堆积。that defines its orientation to be vertical – all child Views (of which it has two) will be stacked vertically. 第一个子元素是另一个LinearLayoutThe first child is another LinearLayout 使用水平方向并且第二个子元素是LinearLayoutthat uses a horizontal orientation and the second child is a LinearLayout 使用垂直方向的。that uses a vertical orientation. 其中每个嵌套LinearLayout包含若干TextViewEach of these nested LinearLayouts contain several TextView 元素,这些元素以其父LinearLayout所定义的方式进行定向。elements, which are oriented with each other in the manner defined by their parent LinearLayout.

现在打开HelloLinearLayout.cs ,并确保它在OnCreate()中加载Resources/Layout/main.axml布局Now open HelloLinearLayout.cs and be sure it loads the Resources/Layout/Main.axml layout in the OnCreate() 付款方式method:

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

SetContentView(int))方法加载资源 ID – 指定的Activity的布局文件,Resources.Layout.Main 引用资源/布局 main.axml布局文件。The SetContentView(int)) method loads the layout file for the Activity, specified by the resource ID – Resources.Layout.Main refers to the Resources/Layout/Main.axml layout file.

运行该应用程序。Run the application. 应该会看到以下内容:You should see the following:

第一垂直排列的应用程序 LinearLayout 屏幕快照Screenshot of app first LinearLayout arranged horizontally, second vertically

请注意,XML 特性如何定义每个视图的行为。Notice how the XML attributes define each View's behavior. 尝试试验 android:layout_weight 的不同值,以查看屏幕房地产如何根据每个元素的权重进行分布。Try experimenting with different values for android:layout_weight to see how the screen real estate is distributed based on the weight of each element. 有关如何LinearLayout的详细信息,请参阅通用布局对象文档See the Common Layout Objects document for more about how LinearLayout 处理 android:layout_weight 特性。handles the android:layout_weight attribute.

referenceReferences

此页面的某些部分是基于 Android 开源项目创建和共享的工作的修改,并根据创造性 Commons 2.5 归属许可证中所述的条款使用。Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.