Проблема линейного описания Android с переносом содержимого

#android #android-linearlayout

#Android #android-linearlayout

Вопрос:

У меня есть следующее линейное описание для элемента списка.

     <ImageView android:id="@ id/icon" 
                   android:src="@android:drawable/ic_input_add"  
                   android:layout_height="wrap_content"  
                   android:layout_width="22px"  
                   android:layout_marginTop="4px"  
                   android:layout_marginRight="4px"  
                   android:layout_marginLeft="1px" />

    <LinearLayout android:id="@ id/linearLayout1"  
                      android:orientation="vertical"  
                      android:layout_height="wrap_content" 
                      android:layout_width="wrap_content">

        <TextView android:layout_width="wrap_content"  
                      android:text="LongerTextView" 
                      android:id="@ id/textView1" 
                      android:layout_height="wrap_content"/>

        <TextView android:layout_width="wrap_content" 
                      android:text="TextView" 
                      android:id="@ id/textView2" 
                      android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout android:id="@ id/linearLayout2" 
                      android:layout_height="match_parent"  
                      android:layout_width="fill_parent" 
                      android:gravity="right">

        <ImageView android:layout_marginTop="4px" 
                       android:src="@android:drawable/ic_input_add"    
                       android:layout_marginLeft="1px"  
                       android:layout_height="wrap_content"   
                       android:layout_marginRight="1px" 
                       android:id="@ id/icon2" 
                       android:layout_width="22px"/>
    </LinearLayout>
  

Предполагается, что макет должен отображать изображение слева и полностью справа, с двумя текстовыми элементами между ними. Это работает нормально, пока текст не становится шире экрана. Как только текст становится длиннее, мой правый значок исчезает с экрана.

Предполагается, что текст переносится на новую строку, и предполагается, что всегда отображаются как левый, так и правый значки.

Как я могу этого добиться?

Ответ №1:

Попробуйте использовать относительные макеты

Внутри относительного макета вы можете указать изображение (например, с id: imgLeft) с помощью layout_alignParentLeft=»true», которое останется слева

Затем другое изображение (imgRight) с layout_alignParentRight=»true», которое останется справа

а затем линейный макет, содержащий тексты с: layout_toLeftOf=»@id /imgRight» и layout_toRightOf=»imgLeft»

Ответ №2:

Я переработал ваш XML в соответствии с вашими требованиями. Используйте следующий код.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <ImageView
        android:id="@ id/icon"
        android:src="@android:drawable/ic_input_add"
        android:layout_height="wrap_content"
        android:layout_width="22px"
        android:layout_marginTop="4px"
        android:layout_marginRight="4px"
        android:layout_marginLeft="1px"></ImageView>
        <LinearLayout
        android:id="@ id/linearLayout2"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:gravity="right"
        android:layout_alignParentRight="true">
        <ImageView
            android:layout_marginTop="4px"
            android:src="@android:drawable/ic_input_add"
            android:layout_marginLeft="1px"
            android:layout_height="wrap_content"
            android:layout_marginRight="1px"
            android:id="@ id/icon2"
            android:layout_width="wrap_content"></ImageView>
    </LinearLayout>
    <LinearLayout
        android:id="@ id/linearLayout1"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@ id/icon"
        android:layout_toLeftOf="@ id/linearLayout2">
        <TextView
            android:layout_width="wrap_content"
            android:text="LongerTextView"
            android:id="@ id/textView1"
            android:layout_height="wrap_content"></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:text="TextView"
            android:id="@ id/textView2"
            android:layout_height="wrap_content"></TextView>
    </LinearLayout>


</RelativeLayout>
  

Не забудьте проголосовать, если мой ответ будет полезен для вас.

Спасибо Дипак

Ответ №3:

IIRC (не тестировался, но я могу сделать примерно через 8 часов) вы можете потерять третье линейное описание (то, которое переносит второй ImageView). Затем установите android: gravity линейного описания, которое переносит текстовые представления в центр.