Anasayfa » Android » Android Layout Weight

Android Layout Weight

Layout Weight

layout weight özelliği bir nesneye içinde bulunduğu alandaki nesneler içerisinde verileceği önceliği belirten özelliktir.  Bu öncelikler 0 ve 1 gibi değerler ile belirtilir demiştik. Şimdi Bir kaç örnek yapalım

Layout Weight 1

layout-Weight

Bu örneğimizde mavi rengimize android:layout_weight=”1″ özelliği verildi. Kırmızı ve yeşil renkler içinandroid:layout_weight=”0″ değeri verildi. Buda mavi renkli text’imize öncelik sağladı

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"> 
 <TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Kırmızı"
 android:background="#aa0000" 
 android:layout_weight="0" 
 android:textColor="#fff"/>
 <TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Yeşil"
 android:background="#00aa00"
 android:layout_weight="0" 
 android:textColor="#fff"/> 
 <TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Mavi"
 android:background="#0000aa"
 android:layout_weight="1"
 android:textColor="#FFFFFF"/>
</LinearLayout>

Layout Weight 2

layout-weight-2

Bu örnekte LinearLayout orientation vertical özelliği kullanarak nesneler dikey sıralandı yukardaki gibi mavi rengimize

 android:layout_weight="1" değeri ile

öncelik tanındı

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_weight="0"
 android:text="Kırmızı"
 android:background="#aa0000"
 android:textColor="#FFFFFF"
 /> 
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_weight="0"
 android:text="Yeşil"
 android:background="#00aa00"
 android:textColor="#FFFFFF"
 /> 
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="Mavi"
 android:background="#0000aa"
 android:textColor="#FFFFFF"
 />
</LinearLayout>

Layout Weight 3

layout-weight-3

Bur örnekte de her rengimiz için android:layout_weight=”1″ değeri ile textlerimizi eşit alana sahip olmaları sağlandı.

Layout Weight 5

layout-weight-5

Bu örneğimizde ise iki adet birinci Linear Layout içerisindeki kırmızı ve yeşil renkten kırmızıya öncelik verildi.

İkinci Linear layout içerisindeki mavi ve siyah textlerden siyaha öncelik verildi

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content">
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="Kırmızı"
 android:background="#aa0000"
 android:textColor="#FFFFFF"/>
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:text="Yeşil"
 android:background="#00aa00"
 android:textColor="#FFFFFF"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1">
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:text="Mavi"
 android:background="#0000aa"
 android:textColor="#FFFFFF"
 />
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:text="Siyah"
 android:background="#000000"
 android:textColor="#FFFFFF"
 android:layout_weight="1" />
 </LinearLayout>
 </LinearLayout>

Layout Weight 6

layout-weight-6

Bu örneğimizde de ibirinci Linear Layout içerisindeki kırmızı ve yeşil renkten kırmızıya öncelik verildi.

İkinci Linear layout içerisindeki mavi ve siyah textlere eşit alana sahip olmaları belirlendi.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"> 
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="Kırmızı"
 android:background="#aa0000"
 android:textColor="#FFFFFF"/> 
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:text="Yeşil"
 android:background="#00aa00"
 android:textColor="#FFFFFF"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1">
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent" 
 android:text="Mavi"
 android:background="#0000aa"
 android:textColor="#FFFFFF" 
 android:layout_weight="1" />
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent" 
 android:text="Siyah"
 android:background="#000000"
 android:textColor="#FFFFFF"
 android:layout_weight="1"
 />
 </LinearLayout>
</LinearLayout>

Birde butonlarla örnek verelim

Layout Weight 7

layout-weight-7

Burada da buton 1 android:layout_weight=”1″ özelliği ile diğer butonlardan daha geniş bir alana sahip olmasını sağladı

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 
 <Button 
 android:text="Buton1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" 
 android:layout_weight="1"
 />
 <Button
 android:text="Buton2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0"
 />
 
 <Button
 android:text="Buton2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0"
 />
 
</LinearLayout>

Layout Weight 8

layout-weight-8

Burada tüm butonlara android:layout_weight=”1″ verildi tüm butonlar yatay layouta göre eşit miktarda konumlandı.

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" > 
 <Button 
 android:text="Buton1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" 
 android:layout_weight="1"
 />
 <Button
 android:text="Buton2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 />
 
 <Button
 android:text="Buton2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 /> 
</LinearLayout>

Layout Weight 9

Birde layout’ları çoğaltarak deniyelim

layout-weight-9

A B C buton layoutları için C butonları’nin layoutuna öncelik tanındı A ve B butonlarının layoutuna eşit layout weight özelliği verildi.

C layoutu içerisindeki butonlardan C1 Butonuna öncelik verildi.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="2"
 android:orientation="horizontal" >
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="A 1" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="A 2" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="A 3" />
 </LinearLayout> 
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="2"
 android:orientation="horizontal" >
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="B 1" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="B 2" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="B 3" />
 </LinearLayout> 
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:orientation="vertical"> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="C 1" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0"
 android:text="C 2" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0"
 android:text="C 3" />
 </LinearLayout> 
</LinearLayout>

Layout Weight 10

nesnelere layout weight değerleri vererek bu nesnelere yüzdesel olarak değer vermiş oluyoruz.

layout-weight-10

 

Birinci Buton   16.6% (1/(1+2+3) * 100)
İkinci Buton    33.3% (2/(1+2+3) * 100)
Üçüncü Buton 50%    (3/(1+2+3) *100)

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 
 <Button 
 android:text="Buton1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" 
 android:layout_weight="1"
 />
 <Button
 android:text="Buton2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="2"
 />
  <Button
 android:text="Buton2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="3"
 /> 
</LinearLayout>

Bu butonları dikey konumda layout içerisinde genişliğine dokunmadan sıralayalım

Layout Weight 11

layout-weight-11

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 >
 
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 
 android:text="A 1" /> 
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 
 android:text="A 2" /> 
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="A 3" />
 
</LinearLayout>

Hesap Makinesi örneğinde daha iyi anlayacaksınız

Layout Weight 12

layout-weight-12

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 > 
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1" >
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="7" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="8" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="9" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="X" /> 
 </LinearLayout> 
 
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1" >
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="4" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="5" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="6" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="-" /> 
 </LinearLayout> 
 
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1" >
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="1" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="2" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="3" /> 

 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:text="+" />
 
 </LinearLayout> 
 
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1" >
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:text="0" /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1.5" 
 android:text="." /> 
 <Button
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:layout_weight="1.5" 
 android:text="=" /> 
 </LinearLayout> 
</LinearLayout>

 

 

Cevapla

E-posta adresiniz yayınlanmayacak. Required fields are marked *

*


8 + = onbir

Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>