如何解决如何以线性布局设置视图,以使其从开始到结束屏幕都适合一行中包含一定数量的元素?
我在LinearLayout中有14条(视图)水平线,它应该适合任何屏幕,所以有什么主意如何设置行之间的宽度以适应任何屏幕尺寸的开始到结束? 查看图片外观: screenshots
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal">
<View
android:id="@+id/one"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/blueTextColor" />
<View
android:id="@+id/two"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginStart="27dp"
android:background="@color/blueTextColor"/>
<View/>
<View/>
<View/>
<View/>
<View/>
<View/>
<View/>
<View/>
<View/>
<View/>
<View
android:id="@+id/fourteen"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginStart="27dp"
android:background="@color/blueTextColor" />
</LinearLayout>
我尝试了一下,但是没用
解决方法
我不确定,但是我认为@GuseeMofiddin的答案无法按您希望的那样工作,因为它会将视图扩展到整个宽度,并且您希望它只是1dp
。您可以使用约束布局和链来实现。示例:
- 像这样创建布局:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal">
<View
android:id="@+id/one"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA" />
<View
android:id="@+id/two"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA" />
<View
android:id="@+id/three"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA" />
<View
android:id="@+id/four"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 现在您必须创建水平链。
首先,将它们全部选中(单击one
,按住 Shift ,然后单击four
)
下一步右键单击并选择:
您的XML文件应如下所示:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal">
<View
android:id="@+id/one"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA"
app:layout_constraintEnd_toStartOf="@+id/two"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<View
android:id="@+id/two"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA"
app:layout_constraintEnd_toStartOf="@+id/three"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/one" />
<View
android:id="@+id/three"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA"
app:layout_constraintEnd_toStartOf="@+id/four"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/two" />
<View
android:id="@+id/four"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0000AA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/three" />
</androidx.constraintlayout.widget.ConstraintLayout>
在两个不同设备上的结果:
(此方法不会在左右边缘上创建视图。为此,只需添加左右边缘之一,然后创建链,但不包括它们)
编辑以评论:在特定视图中添加底部和顶部边距您必须将height
设置为0dp
,并将顶部和底部约束设置为父级的顶部和底部。该视图应如下所示:
<View
android:id="@+id/two"
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#0000AA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/three"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/one"
app:layout_constraintTop_toTopOf="parent"
/>
,
您只需要设置
android:weightSum="14"
到您的父级LinearLayout和
android:layout_weight="1"
查看您的孩子的意见,请尝试此操作
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:weightSum="14">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" />
</LinearLayout>
</RelativeLayout>
快乐编码!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。