2013-08-29 3 views
11

Я пытаюсь разработать приложение для Android, но у меня возникли проблемы с дизайном графического интерфейса. Следующая часть снимка экрана - моя проблема (красные и синие линии были созданы с помощью параметров отладки Android).Запретить просмотр перекрытий в RelativeLayout

my problem

Это код:

<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> 

Как вы можете видеть, TextView MYTEXT перекрывает ImageButton button_edit. Простым решением было бы использовать LinearLayout вместо RelativeLayout. Проблема заключается в том, что:

  1. мне нужна кнопка редактирования справа
  2. мне нужен текст, чтобы заполнить остальную часть макета (слева от редактирования кнопки очевидно)

Я также попытался установить layout_width myText на «fill_parent», но он заполнит весь остальной экран слева от кнопки редактирования.

Ожидаемое поведение будет MYTEXT увеличить его высоту (став две линии TextView) вместо перекрывающихся «button_edit»

Может кто-нибудь мне помочь? Благодаря

+0

Почему вы хотите использовать «RelativeLayout», когда это может быть достигнуто с помощью «LinearLayout»? – sriramramani

ответ

19

использование layout_toLeftOf в макете TextView

так:

<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

Спасибо! он отлично работает! – Gnufabio

+0

Что делать, если мы хотим достичь этого с помощью динамического создания представлений? –

0

Другой способ сделать это, чтобы использовать Android: layout_toEndOf атрибут

<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" /> 

Смежные вопросы