вес = 1 и Android: layout_width = "0dp" заставляет мой вид исчезнуть - PullRequest
0 голосов
/ 24 сентября 2019

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

enter image description here

Я пробовал этот макет:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/my_padding"
    android:paddingBottom="@dimen/my_padding"
    android:paddingLeft="@dimen/my_padding2"
    android:paddingRight="@dimen/my_padding2"
    android:background="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal">

  <ImageView
      android:id="@+id/Icon"
      android:layout_width="@dimen/icon_size"
      android:layout_height="@dimen/icon_size"
      android:layout_margin="@dimen/icon_margin"
      android:layout_gravity="center_horizontal|center_vertical"
      android:contentDescription="@null"
      android:importantForAccessibility="no"
      tools:ignore="UnusedAttribute" />
  <TextView
      android:id="@+id/Text"
      style="@style/ActionItemStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginStart="@dimen/margin_between_icon_and_text"
      android:layout_marginLeft="@dimen/margin_between_icon_and_text"
      android:layout_gravity="center_vertical"
      android:ellipsize="end"
      android:gravity="start|center_vertical"
      android:maxLines="3"
      android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
  <com.my.Chip
      android:id="@+id/highlight_chip"
      style="@style/Widget.Chip.Suggestive"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_marginStart="@dimen/margin_between_highlight_chip_and_text"
      android:layout_marginLeft="@dimen/margin_between_highlight_chip_and_text"
      android:layout_gravity="end|center_vertical"
      android:visibility="visible" />
</LinearLayout>

означает, что у меня чип с весоми layout_gravity, но чип не виден.

enter image description here

как я могу это исправить?

Странно то, что у меня естьпробовал

<com.my.Chip
      android:id="@+id/highlight_chip"
      style="@style/Widget.GoogleMaterial.Chip.Suggestive"
      android:layout_width="10dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="@dimen/margin_between_highlight_chip_and_text"
      android:layout_marginLeft="@dimen/margin_between_highlight_chip_and_text"
      android:layout_gravity="end|center_vertical"
      android:visibility="visible" />

без веса, а чип все равно не показывает

Ответы [ 2 ]

2 голосов
/ 24 сентября 2019

Вам не нужно устанавливать layout_weight в вашем com.my.Chip

Попробуйте установить android:layout_weight="1" на вашем TextView вместо вашего com.my.Chip

, а такжеизмените android:layout_width="0dp" в вашем TextView

Вы получите что-то вроде этого

enter image description here

Попробуйте это

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/my_padding"
    android:paddingBottom="@dimen/my_padding"
    android:paddingLeft="@dimen/my_padding2"
    android:paddingRight="@dimen/my_padding2"
    android:background="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal">

  <ImageView
      android:id="@+id/Icon"
      android:layout_width="@dimen/icon_size"
      android:layout_height="@dimen/icon_size"
      android:layout_margin="@dimen/icon_margin"
      android:layout_gravity="center_horizontal|center_vertical"
      android:contentDescription="@null"
      android:importantForAccessibility="no"
      tools:ignore="UnusedAttribute" />
  <TextView
      android:id="@+id/Text"
      style="@style/ActionItemStyle"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_marginStart="@dimen/margin_between_icon_and_text"
      android:layout_marginLeft="@dimen/margin_between_icon_and_text"
      android:layout_gravity="center_vertical"
      android:ellipsize="end"
      android:gravity="start|center_vertical"
      android:maxLines="3"
      android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
  <com.my.Chip
      android:id="@+id/highlight_chip"
      style="@style/Widget.Chip.Suggestive"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="@dimen/margin_between_highlight_chip_and_text"
      android:layout_marginLeft="@dimen/margin_between_highlight_chip_and_text"
      android:layout_gravity="end|center_vertical"
      android:visibility="visible" />
</LinearLayout>
0 голосов
/ 24 сентября 2019

Проблема в том, что, хотя вы предоставляете атрибут веса только для просмотра текста, он не будет отображать ожидаемый результат.Вам необходимо использовать атрибут «weight_sum», чтобы создать отверстие для всех трех представлений, которые вы использовали внутри группы представлений, т.е. изображения, просмотра текста и чипа.поэтому решение будет выглядеть так ->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp"
    android:weightSum="2">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="0.2"
        android:background="@drawable/ic_launcher_background"
        tools:ignore="ContentDescription" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1.6"
        android:background="@android:color/holo_green_light"
        android:text="Demo Text" />

    <com.google.android.material.chip.Chip
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:layout_weight="0.2"
        android:background="@drawable/ic_launcher_background" />

</LinearLayout>
...