Выравнивание вправо и влево с линейным или относительным расположением с усеченным текстом? - PullRequest
6 голосов
/ 14 августа 2010

Я пытаюсь создать вид списка, который выглядит следующим образом: альтернативный текст http://pici.se/pictures/hjFksZvLx.jpg

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

Я думал, что это должно быть просто сделать.Но это не так.

Сначала я попробовал с Linearlayout:

<?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:orientation="vertical"
        android:id="@+id/L1"
        android:layout_height="fill_parent"
        android:layout_weight="0.8"
        android:layout_width="0dip">
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:ellipsize="end"
            android:scrollHorizontally="true"
            android:text="Long text Long text Long text Long text Long text Long text Long text" />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:ellipsize="end"
            android:scrollHorizontally="true"
            android:text="Short text" />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/L2"
        android:layout_gravity="top|right"
        android:layout_height="fill_parent"
        android:layout_weight="0.2"
        android:layout_width="0dip">
        <TextView
            android:id="@+id/text4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="12:12" />
        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="64%"
            android:layout_gravity="right" />
    </LinearLayout>
</LinearLayout>

Я возился со свойством веса, но, похоже, это не помогло.Правая часть вообще не выравнивается вправо.

А потом я попытался с Relativelayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
    <TextView
        android:id="@+id/text1"
        android:layout_height="wrap_content"
        android:lines="1"
        android:scrollHorizontally="true"
        android:layout_alignParentTop="true"
        android:text="Long text Long text Long text Long text Long text Long text Long text"
        android:layout_alignParentLeft="true"
        android:ellipsize="end"
        android:layout_width="275dp" />
    <TextView
        android:id="@+id/text2"
        android:layout_height="wrap_content"
        android:lines="1"
        android:ellipsize="end"
        android:scrollHorizontally="true"
        android:text="Long text Long text Long text Long text Long text Long text Long text"
        android:layout_below="@+id/text1"
        android:layout_alignParentLeft="true"
        android:layout_width="275dp" />
    <TextView
        android:id="@+id/text3"
        android:layout_height="wrap_content"
        android:text="12:12"
        android:layout_gravity="right"
        android:layout_alignParentRight="true"
        android:layout_alignBaseline="@+id/text1"
        android:layout_width="40dp"
        android:layout_alignRight="@+id/text4" />
    <TextView
        android:id="@+id/text4"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="64%"
        android:layout_alignParentRight="true"
        android:layout_alignBaseline="@+id/text2"
        android:layout_width="40dp" />
</RelativeLayout>

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

1 Ответ

9 голосов
/ 14 августа 2010

У меня та же проблема, что и у вас несколько дней назад.Некоторые моменты:

  • Я использовал android:singleLine="true" вместо android:lines="1".Честно говоря, я не знаю разницу.Я думаю, один раз устарел.

  • Чтобы выровнять по правому краю: android:gravity="right"

  • Чтобы избежать наложения, я поместил ваши TextViews слева внутриLinearLayout и я добавляю его android:layout_marginRight="50dp"

Мой xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <LinearLayout android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_marginRight="50dp">

        <TextView
            android:id="@+id/text1"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:scrollHorizontally="true"
            android:layout_alignParentTop="true"
            android:text="Long text Long text Long text Long text Long text Long text Long text"
            android:layout_alignParentLeft="true"
            android:ellipsize="end"
            android:layout_width="275dp" />
        <TextView
            android:id="@+id/text2"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:scrollHorizontally="true"
            android:text="Long text Long text Long text Long text Long text Long text Long text"
            android:layout_below="@+id/text1"
            android:layout_alignParentLeft="true"
            android:layout_width="275dp" />
    </LinearLayout>

    <TextView
        android:id="@+id/text3"
        android:layout_height="wrap_content"
        android:text="12:12"
        android:layout_gravity="right"
        android:layout_alignParentRight="true"
        android:layout_alignBaseline="@+id/text1"
        android:layout_width="40dp"
        android:layout_alignRight="@+id/text4" />
    <TextView
        android:id="@+id/text4"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="64%"
        android:layout_alignParentRight="true"
        android:gravity="right"
        android:layout_below="@id/text3"
        android:layout_width="40dp" />
</RelativeLayout>

Использование android:layout_marginRight="50dp" во избежание дублирования не является правильным выбором,но на данный момент это единственное решение, которое я нашел.

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