gpt4 book ai didi

android - 在膨胀布局android中的每个项目布局之间添加空间

转载 作者:搜寻专家 更新时间:2023-11-01 09:36:51 25 4
gpt4 key购买 nike

我做膨胀布局。我想在布局中的每个项目之间留出间距。喜欢下图

Left side what i want to do, the right what i do

这是我在 xml 中的主要代码。父布局

<LinearLayout
android:id="@+id/mLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#F2F2F2"
android:orientation="vertical"
android:padding="2dp"/>

这是我的inflate布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="3dp"
android:layout_marginTop="5dp"
android:background="@color/white_pure">


<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical">

<TextView
android:textStyle="bold"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>

<TextView
android:layout_width="wrap_content"
android:textSize="9sp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>


</LinearLayout>


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="right"
android:layout_marginRight="5dp"
android:textColor="@color/blueTransfer"/>

</LinearLayout>

我怎样才能得到像图像(左侧)。在膨胀布局中的每个项目之间有间距。希望有人能帮助我。

最佳答案

一个解决方案是像这样更改项目布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#fff"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="aaaaaaaaaaaa"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="bbbbbbbbbb"
android:textColor="#000"
android:textSize="9sp"
/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="3"
android:gravity="right|center"
android:text="Details"
android:textColor="#00f"
/>
</LinearLayout>

</LinearLayout>

enter image description here

另一种解决方案是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:background="#fff"
android:orientation="horizontal"
android:padding="3dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="aaaaaaaaaaaa"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="bbbbbbbbbb"
android:textColor="#000"
android:textSize="9sp"
/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="3"
android:gravity="right|center"
android:text="Details"
android:textColor="#00f"
/>
</LinearLayout>

然后,如果您使用 ListView,则可以使用 android:divider 增加 ListView 项之间的间距 https://stackoverflow.com/a/5309871/5381331

或者对于 RecyclerView:How to add dividers and spaces between items in RecyclerView?

关于android - 在膨胀布局android中的每个项目布局之间添加空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42429764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com