2013-11-20 5 views
0

У меня есть относительный макет, содержащий два LinearLayout. Поэтому, когда я setText (идентификатор companyText) и его более длинный текст он идет выше в другом TextView (идентификатор companyDistance)как перейти к следующей строке в android xml

Когда companyName имеет более длинный текст я хочу перейти на следующую строку, а не идти avobe на второй макет для более ясно прилагается фото

enter image description here

Но я хочу это будет как в этой фотографии

enter image description here

Вот мой main.xml файл

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/mainLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:padding="10dp"> 
    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true"> 
     <ImageView 
       android:id="@+id/companyImageView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/secret_logo_small" 
       android:layout_marginRight="10dp"/> 
     <TextView 
       android:id="@+id/companyName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@id/companyImageView" 
       android:textColor="@android:color/white" 
       android:layout_gravity="center" 
       android:text="Secret"/> 
    </LinearLayout> 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_alignParentRight="true"> 
     <TextView 
       android:id="@+id/companyDistance" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="@android:color/white" 
       android:layout_marginTop="7dp" 
       android:textSize="18sp" 
       android:text="154" 
       android:layout_gravity="right"/> 

     <Button 
       android:id="@+id/companyPercentImage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_below="@id/companyDistance" 
       android:textColor="@android:color/white" 
       android:text="@string/add" 
       android:paddingLeft="20dp" 
       android:paddingRight="20dp" 
       android:textSize="11sp" 
       android:background="@drawable/button_indicator_dagavit" 
       android:drawableLeft="@drawable/small_green_button_add_icon"/> 
    </LinearLayout> 
</RelativeLayout> 

ответ

1

Не использовать RelativeLayout использовать LinearLayout.

Вот полный ответ на ваш вопрос. Удачи.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/mainLayout" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:padding="10dp"> 
    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
       android:layout_weight="1" 
      android:layout_alignParentLeft="true"> 
     <ImageView 
       android:id="@+id/companyImageView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/secret_logo_small" 
       android:layout_marginRight="10dp"/> 
     <TextView 
       android:id="@+id/companyName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@id/companyImageView" 
       android:textColor="@android:color/white" 
       android:layout_gravity="center" 
       android:text="Secret"/> 
    </LinearLayout> 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_alignParentRight="true"> 
     <TextView 
       android:id="@+id/companyDistance" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="@android:color/white" 
       android:layout_marginTop="7dp" 
       android:textSize="18sp" 
       android:text="154" 
       android:layout_gravity="right"/> 

     <Button 
       android:id="@+id/companyPercentImage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_below="@id/companyDistance" 
       android:textColor="@android:color/white" 
       android:text="@string/add" 
       android:paddingLeft="20dp" 
       android:paddingRight="20dp" 
       android:textSize="11sp" 
       android:background="@drawable/button_indicator_dagavit" 
       android:drawableLeft="@drawable/small_green_button_add_icon"/> 
    </LinearLayout> 
</LinearLayout> 
+0

это может сработать, но более эффективно использовать относительный макет вместо линейного макета –

+0

@pmb, если это вам помогло, примите этот ответ. Это поможет другим пользователям с аналогичной проблемой. – Michael

0

Поместите это в TextView:

android:maxLines="10" 

Затем используйте \n в тексте Вашего TextView.

maxLines делает TextView максимально таким большим количеством линий. Вы можете выбрать другой номер :)

+0

спасибо за ваш ответ, но это не очень хорошее решение для меня на этот раз. – pmb

+0

Добавьте '\ t' для вкладки и '\ n' для новой строки –

0

Во-первых, добавить идентификатор к вашему первому LinearLayout:

<LinearLayout 
    android:id="@+id/left_linear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true"> 

И в вашей второй LinearLayout вам нужно добавить атрибут

android:layout_toRightOf="@id/left_linear 

так что ваш XML будет выглядят так:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@id/left_linear"> 
Смежные вопросы