Xamarin.Android LinearLayout

LinearLayoutViewGroup 자식 표시 View 선형 방향의 요소(세로 또는 가로)입니다.

를 과도하게 사용하는 LinearLayout것에 주의해야 합니다. 여러 LinearLayouts 중첩을 시작하는 경우 다음을 사용하는 것이 좋습니다. 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(2개 포함)은 세로로 쌓입니다. 첫 번째 자식은 다른 자식입니다. LinearLayout 가로 방향을 사용하고 두 번째 자식은 입니다. LinearLayout 세로 방향을 사용하는 LinearLayout. TextView 는 부모가 LinearLayout정의한 방식으로 서로 지향하는 요소입니다.

이제 HelloLinearLayout.cs 열고 다음에서 Resources/Layout/Main.axml 레이아웃을 로드하는지 확인합니다.OnCreate() 메서드:

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

) 메서드는 리소스 ID Resources.Layout.Main 로 지정된 레이아웃 파일을 Activity로드합니다. 리소스/레이아웃/Main.axml 레이아웃 파일을 참조합니다.SetContentView(int)

애플리케이션을 실행합니다. 다음이 표시되어야 합니다.

Screenshot of app first LinearLayout arranged horizontally, second vertically

XML 특성이 각 뷰의 동작을 정의하는 방법을 확인합니다. 각 요소의 가중치에 따라 화면 부동산이 어떻게 분산되는지 확인하기 위해 android:layout_weight 다양한 값을 실험해 보세요. 방법에 대한 자세한 내용은 일반 레이아웃 개체 문서를 참조하세요.LinearLayout 는 특성을 처리합니다 android:layout_weight .

참조

이 페이지의 일부는 Android 오픈 소스 프로젝트에서 만들고 공유하고 Creative Commons 2.5 특성 라이선스에 설명된 용어에 따라 사용되는 작업을 기반으로 하는 수정 사항입니다.