【问题标题】:Cannot spread children views to the sides of their parent linear layout无法将子视图传播到其父线性布局的两侧
【发布时间】:2019-09-18 20:32:27
【问题描述】:

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

1) 一个GroupView,左边有一个textView

2) 右侧的imageView

它们之间有一个最小的间隙,所以如果 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>

但结果不会将孩子们的观点推到一边,也不会保持他们之间的差距有利于图像。

我该如何解决这个问题?

【问题讨论】:

标签: android android-layout android-linearlayout gravity


【解决方案1】:

你可以通过给你的文本视图设置权​​重来修复它

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

【讨论】:

  • 这将解决 image#1,但是 image #2 呢?我希望文本被截断
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多