【问题标题】:weight=1 and android:layout_width="0dp" makes my view disappearweight=1 和 android:layout_width="0dp" 使我的视图消失
【发布时间】:2019-09-24 08:05:18
【问题描述】:

当我有一个图像时,我想创建一个布局,文本与布局的左侧对齐。 和一个向左对齐的芯片。

我试过这个布局:

<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的芯片,但是没有看到芯片。

我该如何解决这个问题?

奇怪的是我试过了

<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" />

没有重量,芯片仍然不显示

【问题讨论】:

  • 为什么要坚持线性布局?

标签: android android-layout android-linearlayout android-layout-weight


【解决方案1】:

您无需在com.my.Chip 中设置layout_weight

尝试在您的TextView 上设置android:layout_weight="1",而不是在您的com.my.Chip

并在您的TextView 中更改android:layout_width="0dp"

你会得到类似这样的输出

试试这个

<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>

【讨论】:

  • 会尝试,但是为什么我把重量放在哪里很重要?
  • @EladBenda 如果你想在com.my.Chip 中设置权重,那么只需将textView 中的width 更改为android:layout_width="wrap_content" 就可以了
  • @NileshRathod,你把重量放在哪里很重要。父级有一个已定义的width,我们希望将其分配给child views。因此,具有最高优先级的子视图获得weight=1,其余视图将获得wrap_contentsome dp value。这很重要,因为当 android 布置视图时,它会将值分配给首先没有关联weight 的值,然后将剩余的值分配给带有weight 的视图。我希望这会有所帮助。
【解决方案2】:

问题是,当您只为文本视图提供“权重”属性时,它不会呈现预期的输出。您需要使用“weight_sum”属性为您在视图组中使用的所有三个视图(即 imageview、textview 和芯片)创建一个开口。 所以解决方案看起来像这样 ->

<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多