View bileşenlerinin yatay veya dikey hatta doğrusal bir şekilde sıralanmaları için seçilen layout türüdür. Çeşitli ekran türlerinde sorunsuz bir şekilde çalışır.
Örnek LinearLayout kodu şu şekildedir:
1 2 3 4 | <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"></LinearLayout> |
Bu kod parçasında android:orientation="horizontal" parametresi ile nesnelerin yatay bir şekilde yerleşeceğini belirtiriz. android:orientation="vertical" parametresi kullanıldığında da nesneneler alt alta dizilmeye başlar.
LinearLayout nesneleri iç içe yerleştirilebilir. En çok kullanılan ve şekline doğrudan etki eden parametrelerden birisi android:layout_width parametresidir. Bu parametreye istenilen sayısal değer girilebildiği gibi wrap_content ve match_parent olmak üzere iki farklı değeri de vardır. wrap_content ile genişlik veya yükseklik değeri içeriğe göre otomatik ayarlanırken match_parent ile içinde bulunduğu kapsayıcının değerini miras alır.
Aşağıdaki örnekte tüm butonların genişliği wrap_content olarak ayarlandığı halde, her biri LinearLayout‘lar içinde bulunsa da farklı davranış şekillerine sahip oldukları görülmektedir. Bunu sağlayan bir diğer önemli parametre ise layout_weight parametresidir. Bu parametre, mevcut kapsayıcı içerisinde kalan boşlukların doldurulup doldurulmayacağını belirtmek için kullanılır.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button2" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button4" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button5" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button7" /> <Button android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button8" /> <Button android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button9" /> </LinearLayout> </LinearLayout> |