首页 > 解决方案 > 无法将子视图传播到其父线性布局的两侧

问题描述

我想创建一个内部有 2 个视图的布局。

1) A左边GroupView有 atextView

2) AimageView向右

它们之间有一个最小的间隙,所以如果 textView 太长,它会截断其内容,但图像永远不会缩小。

这是我的尝试:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

  <myTextView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical|left" />


  <ImageView
      android:id="@+id/my_image"
      android:layout_width="18dp"
      android:layout_height="18dp"
      android:layout_marginTop="@dimen/my_margin_vertical"
      android:layout_marginBottom="@dimen/my_margin_vertical"
      android:layout_marginEnd="@dimen/my_margin_end"
      android:layout_marginRight="@dimen/my_margin_end"
      android:layout_gravity="center_vertical|right"
      app:srcCompat="@drawable/my_color_18_vd" />
</LinearLayout>

但结果并没有将孩子们的观点推到一边,也没有让他们之间的差距有利于形象。

我怎样才能解决这个问题?

在此处输入图像描述

在此处输入图像描述

标签: androidandroid-layoutandroid-linearlayoutgravity

解决方案


您可以通过为 textview 设置权重来修复它

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

  <myTextView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
     android:layout_weight="1"
 android:layout_marginEnd="10dp"
      android:layout_gravity="center_vertical|left" />


  <ImageView
      android:id="@+id/my_image"
      android:layout_width="18dp"
      android:layout_height="18dp"
      android:layout_marginTop="@dimen/my_margin_vertical"
      android:layout_marginBottom="@dimen/my_margin_vertical"
      android:layout_marginEnd="@dimen/my_margin_end"
      android:layout_marginRight="@dimen/my_margin_end"
      android:layout_gravity="center_vertical|right"
      app:srcCompat="@drawable/my_color_18_vd" />
</LinearLayout>

推荐阅读