android - 无法将子视图传播到其父线性布局的两侧
问题描述
我想创建一个内部有 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>
但结果并没有将孩子们的观点推到一边,也没有让他们之间的差距有利于形象。
我怎样才能解决这个问题?
解决方案
您可以通过为 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>
推荐阅读
- javascript - if/else Javascript 语句返回相反的值
- ruby-on-rails - 如何在 Ruby on Rails 中使用数组对键进行分组和合并
- mysql - MySQL存储过程`错误代码:1064`在运行时
- python - Python pandas:根据较低级别的间距单列到多列
- json - AWS powershell commandlet (Write-CWDashboard) 输出消息“应该与 oneOf 中的一个模式完全匹配”
- python - 为什么当我使用 multiprocessing.Process 运行时 ZeroMQ 无法通信?
- c++ - 使用 Stroustrup 示例的 condition_vairable::wait_for() 问题
- loops - 如何循环寄存器输出的主机变量
- spring-webflux - 从 Mono.fromCallable 返回 Mono.empty()
- java - Apache Camel 到 Firebase 云消息传递 API 400 错误请求错误 NOT_A_JSON_REQUEST