Android - Как создать макет переменной ширины с изображением на любом конце и текстом в середине - PullRequest
0 голосов
/ 28 февраля 2012

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

Если длина любого TextView достаточно мала, я хочу, чтобы вся выноска была достаточно широкой, чтобы показать текст;если он слишком длинный, я хочу, чтобы он был обрезан символом '...'.

Вот пример XML-кода, с которым я имею дело (для ясности я удалил все поля, отступы и т. д.):

<RelativeLayout android:id="@+id/callout" 
  android:layout_width="wrap_content" 
  android:layout_height="54dp">
    <ImageView android:id="@+id/callout_img_left" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/>
    <RelativeLayout android:id="@+id/callout_info" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"     
      android:layout_toRightOf="@id/callout_img_left">
        <TextView android:id="@+id/callout_name" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:ellipsize="end" 
          android:singleLine="true" />
        <TextView android:id="@+id/callout_text" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:ellipsize="end" 
          android:singleLine="true"
          android:layout_below="@id/callout_name" />
    </RelativeLayout>
    <ImageView android:id="@+id/callout_img_right"
      android:layout_width="48dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/callout_info"/>
</RelativeLayout>

Изначально у меня было изображение справа, используя android:layout_alignParentRight="true", но это заставило всю выноску заполнить его доступной шириной.Приведенный выше XML работает для короткого текста, но если текст слишком длинный, он выталкивает (сжимает?) Изображение справа.

1 Ответ

1 голос
/ 28 февраля 2012

Возможно, вложенный LinearLayout с нулевой шириной подойдет:

<LinearLayout android:id="@+id/callout"
  android:orientation="horizontal"
  android:layout_width="wrap_content" 
  android:layout_height="54dp">

    <ImageView android:id="@+id/callout_img_left" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/>
    <LinearLayout
      android:layout_weight="1"
      android:layout_width="0dp" 
      android:layout_height="wrap_content" />
        <TextView android:id="@+id/callout_name" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:ellipsize="end" 
          android:singleLine="true" />
        <TextView android:id="@+id/callout_text" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:ellipsize="end" 
          android:singleLine="true" />
    </LinearLayout>
    <ImageView android:id="@+id/callout_img_right"
      android:layout_width="48dp"
      android:layout_height="wrap_content" />
</LinearLayout>

Если вы не возражаете против двух текстовых представлений, всегда имеющих одинаковую ширину, вы можете обойтись без вложенного LinearLayout и просто дать каждому TextView ширину 0dp и вес 1.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...