Я создал ListItem, который используется в моем приложении на основе REST для заполнения ListView. Предварительный просмотр в студии Android выглядит правильно, но само приложение отображается странно. Я испортил match_parent, а также wrap_content для TextViews в представленном ниже макете.
По сути, я стремлюсь к макету, аналогичному интерфейсу GMail.
Кстати, lbl_date и lbl_m900 должны иметь приоритет и никогда не должны сокращаться или скрываться.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/lbl_title"
android:layout_weight=".9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_marginStart="10dp"
android:text="Title"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/lbl_date"
android:layout_weight=".1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:gravity="right"
android:text="Date"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="@color/blue_dark"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/lbl_details"
android:layout_weight=".9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_marginStart="10dp"
android:text="Details"
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/grey"/>
<TextView
android:id="@+id/lbl_mileage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="right"
android:text="0.0"
android:textColor="@color/blue_dark"/>
</LinearLayout>
</LinearLayout>
Предварительный просмотр в Android Studio
Просмотреть в эмуляторе / устройстве
2 ответа
При использовании LinearLayout и атрибута layout_weight , если ориентация - горизонтальная , layout_width должно быть установлено на 0dp, если ориентация - по вертикали , layout_height должна быть установлена на 0dp.
Попытаться изменить
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/lbl_title"
android:layout_weight=".9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_marginStart="10dp"
android:text="Title"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/lbl_date"
android:layout_weight=".1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:gravity="right"
android:text="Date"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="@color/blue_dark"/>
</LinearLayout>
Надеюсь, это сработает.
Приведенный ниже код сохранит текст даты с правой стороны.
Используйте width = wrap_Content и weight = 0 (по умолчанию), чтобы ваш компонент не был усечен или растянут функцией Weight.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/lbl_title"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_marginStart="10dp"
android:text="Title"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/lbl_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Date"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="@color/blue_dark"/>
</LinearLayout>
Похожие вопросы
Новые вопросы
android
Android — это мобильная операционная система Google, используемая для программирования или разработки цифровых устройств (смартфонов, планшетов, автомобилей, телевизоров, одежды, очков, IoT). Для тем, связанных с Android, используйте теги, специфичные для Android, такие как android-intent, android-activity, android-adapter и т. д. Для вопросов, отличных от разработки или программирования, но связанных с Android framework, используйте эту ссылку: https://android .stackexchange.com.