2013-08-29 14 views
11

Próbuję utworzyć aplikację dla systemu Android, ale mam pewne problemy z graficznym interfejsem użytkownika. Następna część ekranu to mój problem (czerwone i niebieskie linie zostały wygenerowane przez opcje debugowania Androida).Zapobieganie nakładaniu się nakładek w RelativeLayout

my problem

Jest to kod:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/caption" 
    android:layout_below="@+id/caption" > 

    <ImageButton 
     android:id="@+id/button_edit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/content_edit" 
     style="?android:attr/borderlessButtonStyle" /> 

    <TextView 
     android:id="@+id/myText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:text="@string/num_placeholder" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 


</RelativeLayout> 

Jak widać, TextView myText przykrywa button_edit ImageButton. Prostym rozwiązaniem byłoby użycie LinearLayout zamiast RelativeLayout. Problem polega na tym, że:

  1. muszę przycisk edycji po prawej
  2. muszę tekst wypełnić resztę układu (po lewej stronie przycisk Edytuj oczywiście)

ja również próbowałem ustawić szerokość_strony myText na "fill_parent", ale wypełnia ona całą resztę ekranu po lewej stronie przycisku edycji.

Oczekiwane zachowanie będzie myText zwiększyć jego wysokość (stając się dwóch linii TextView) zamiast nakładających „button_edit”

Czy ktoś może mi pomóc? Dzięki

+0

Dlaczego chcesz użyć 'RelativeLayout', kiedy można to osiągnąć za pomocą' LinearLayout'? – sriramramani

Odpowiedz

19

użycie layout_toLeftOf w układzie TextView

tak:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/caption" 
android:layout_below="@+id/caption" > 

<ImageButton 
    android:id="@+id/button_edit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/content_edit" 
    style="?android:attr/borderlessButtonStyle" /> 

<TextView 
    android:id="@+id/myText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:text="@string/num_placeholder" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_toLeftOf="@+id/button_edit" /> 

+0

Dziękujemy! działa idealnie! – Gnufabio

+0

Co jeśli chcemy to osiągnąć dzięki dynamicznemu tworzeniu widoków? –

0

Innym ze sposobów jest użycie android: layout_toEndOf przypisują

<ImageButton 
    android:id="@+id/button_edit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/content_edit" 
    style="?android:attr/borderlessButtonStyle" 
    android:layout_toEndOf="@+id/myText" /> 

<TextView 
    android:id="@+id/myText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:text="@string/num_placeholder" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

Powiązane problemy