2013-05-16 14 views

Odpowiedz

38

Oto układ z 4 przycisków w różnych kolorach:

<?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="match_parent" 
    android:layout_weight="1.0" 

    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" 
     android:layout_weight="1.0" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/holo_green_light" 
     android:layout_weight="1.0" 
     android:text="Button" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1.0" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/holo_blue_light" 
     android:layout_weight="1.0" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/holo_red_light" 
     android:layout_weight="1.0" 
     android:text="Button" /> 

</LinearLayout> 
</LinearLayout> 

enter image description here

+3

To da „zagnieżdżone ciężary są złe dla wydajności”, żaden sposób tego uniknąć? –

+0

dlaczego przyciski nie są klikalne? –

+0

, ponieważ trzeba przypisać detektory kliknięć do przycisków. –

2

Poniższy kod podzieli twój ekran na cztery części.

<?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 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 
    </LinearLayout> 
</LinearLayout> 


<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 
    </LinearLayout> 
</LinearLayout> 

używać żadnych "view" zamiast "LinearLayout", ale z wykorzystania jego atrybuty w tym.

Powiązane problemy