[英]Cannot spread children views to the sides of their parent linear layout
我想创建一个内部有 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>
但结果并没有将孩子们的观点推到一边,也没有让他们之间的差距有利于形象。
我怎样才能解决这个问题?
您可以通过将权重设置为 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.