Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
LinearLayout je ViewGroup zobrazující podřízené View prvky v lineárním směru, buď svisle, nebo vodorovně.
Měli byste být opatrní ohledně nadměrného používání LinearLayout.
Pokud začnete vnořovat více LinearLayouts, můžete zvážit použití RelativeLayout Místo toho.
Spusťte nový projekt s názvem HelloLinearLayout.
Otevřete Prostředky/Layout/Main.axml a vložte následující:
<?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>
Pečlivě zkontrolujte tento kód XML. Kořen je k dispozici. LinearLayout definuje jeho orientaci na svislou – všechny podřízené Viewobjekty (z nichž mají dva) budou skládané svisle. První dítě je další LinearLayout která používá vodorovnou orientaci a druhá podřízená položka je LinearLayout používá svislou orientaci. Každá z těchto vnořených LinearLayouts obsahuje několik TextView prvky, které jsou orientované na sebe způsobem definovaným jejich nadřazeným LinearLayout.
Teď otevřete HelloLinearLayout.cs a ujistěte se, že načte rozložení Resources/Layout/Main.axml v OnCreate() Metoda:
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
}
Metoda SetContentView(int)) načte soubor rozložení pro Activity, určený ID prostředku – Resources.Layout.Main odkazuje na Resources/Layout/Main.axml rozložení souboru.
Aplikaci spusťte. Měli byste vidět následující:
Všimněte si, jak atributy XML definují chování jednotlivých zobrazení. Zkuste experimentovat s různými hodnotami android:layout_weight , abyste viděli, jak se reality obrazovky distribuují na základě váhy jednotlivých prvků. Další informace o tom, jak na to, najdete v dokumentu Společné objekty rozložení.LinearLayoutandroid:layout_weight zpracovává atribut.
Reference
Části této stránky jsou upraveny na základě práce vytvořené a sdílené opensourcový projekt Androidu a používají se podle podmínek popsaných v licenci Creative Commons 2.5 Atribution License.
