2011-07-05 8 views
9

W tej chwili mam dwa widoki tekstowe jeden na wyrównanie po lewej stronie względnego układu i jeden wyrównany w prawo. Tekst po lewej jest znacznie dłuższy niż po prawej. W niektórych przypadkach tekst po lewej stronie ma dwie linie. W takim przypadku tekst pokrywa się z wyrównanym tekstem w prawo.Jak zapobiec nakładaniu się widoków w układzie względnym?

Czy jest mimo to, aby temu zapobiec? Czy istnieje sposób, aby powiedzieć, czy tekst po lewej stronie jest dwie linie umieścić tekst po prawej stronie w drugim wierszu?

Mam problemy z nazwą i czasem tu:

<RelativeLayout android:layout_width="wrap_content" android:id="@+id/relativeLayout" android:layout_height="wrap_content"> 

    <TextView android:text="TextView" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10sp" android:textStyle="bold" android:textSize="16sp"></TextView> 
    <TextView android:layout_width="wrap_content" android:id="@+id/address" android:text="address" android:layout_height="wrap_content" android:layout_below="@+id/name" android:layout_alignLeft="@+id/name" android:layout_marginLeft="30sp"></TextView> 
    <TextView android:layout_width="wrap_content" android:layout_toRightOf="@+id/address" android:text="" android:layout_height="wrap_content" android:layout_alignTop="@+id/address" android:layout_alignBottom="@+id/address" android:id="@+id/crossStreet"></TextView> 
    <TextView android:layout_width="wrap_content" android:id="@+id/time" android:text="Time" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10sp"></TextView> 

</RelativeLayout> 

Odpowiedz

16

mógłbyś zamiast zawierać te dwa TextView w poziomym LinearLayout (która sama w sobie wciąż być dzieckiem swojej RelativeLayout). Następnie przypisz odpowiednie atrybuty wagi do każdego z dwóch widoków TextView w tym LinearLayout.

+0

niesamowite. zadziałało. Ustawiłem również czas na prawo od układu liniowego, by móc go później wykorzystać. – mergesort

+0

Mam się bawić z ciężarkami? Mam .35 i .65, aby to działało, ale .4 i .6 przyniosły śmieszne rezultaty. – mergesort

0

Proszę sprawdzić poniższy kod bardziej przydatne dla Ciebie.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <LinearLayout android:layout_width="fill_parent" 
     android:weightSum="1.0" android:id="@+id/relativeLayout" 
     android:layout_height="wrap_content"> 
     <TextView android:text="TextView1 hhhhh hhhhhhhh hhhhhhhh hhhhhhh hhhhhh" 
      android:id="@+id/name" android:layout_weight="0.5" 
      android:layout_width="0dip" android:layout_height="wrap_content" 
      android:layout_marginLeft="10sp" android:textStyle="bold" 
      android:textSize="16sp"></TextView> 
     <TextView android:layout_width="0dip" android:layout_weight="0.5" 
      android:id="@+id/time" android:text="Textview4" 
      android:layout_height="wrap_content" android:layout_alignParentRight="true" 
      android:layout_marginRight="10sp"></TextView> 
    </LinearLayout> 
</LinearLayout> 
19

Aby uzyskać taki sam wynik używając RelativeLayout skorzystaj android:layout_toLeftOf="@+id/right_text". Ustawi prawą krawędź tego widoku po lewej stronie danego ID widoku kotwicy.

proszę pójść za tym przykładem:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/margin_material_small" 
    android:paddingRight="@dimen/margin_material_small"> 
    <TextView 
     android:id="@+id/long_text" 
     style="@style/Base.TextAppearance.AppCompat.Title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/right_text" 
     android:singleLine="true" 
     tools:text="Some text field that will definitely overlap with another one" /> 
    <TextView 
     android:id="@+id/right_text" 
     style="@style/Base.TextAppearance.AppCompat.Display1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:singleLine="true" 
     tools:text="10.34" /> 
    <TextView 
     android:id="@+id/subtext" 
     style="@style/Base.TextAppearance.AppCompat.Medium" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/long_text" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/right_text" 
     android:singleLine="true" 
     tools:text="Some other text slightly smaller"/> 
</RelativeLayout> 


wyniku powyższego układu:

Result of attached example

+7

To powinno być zaakceptowaną odpowiedzią. –

+1

Jest to zdecydowanie lepsze od obecnie akceptowanej odpowiedzi. Nie ma potrzeby zagnieżdżania innego kontenera (układu liniowego) we względnym układzie. Chcesz maksymalnie zmniejszyć pojemniki zagnieżdżające. – PentaPenguin

0
<RelativeLayout 
     android:id="@+id/relativeParent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/titleLeft" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_toLeftOf="@+id/imageRight" 
      android:layout_alignParentStart="true" /> 

     <ImageView 
      android:id="@+id/imageRight" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:src="@drawable/image" /> 
</RelativeLayout> 



android:layout_toLeftOf="@+id/imageRight" 
android:layout_alignParentStart="true" 

wystarczyły bez dodawania dodatkowych LinearLayout

0

Najlepsze rozwiązanie:

android:layout_toStartOf="@id/item_on_the_right" 
Powiązane problemy