2016-09-23 93 views
0

下面提到的布局是ListView中的一个项目的模型。正如你所看到的那样,这三个TextView是分开的,因为每个TextView占据整个行空间的.3如何在列表视图中的项目之间添加空间

问题是,当我将项目添加到ListView时,我发现三个TextView只是相互关联。例如,假设我要项添加到ListView包含以下内容:

Adam USA M 

我希望看到一些空格分开每个TextView的屏幕上该行,但发生的事情是,我得到的东西像如下:

AdamUSAM 

为什么会发生这种情况,以及如何解决它?

model_view

<?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="match_parent"> 

    <TextView 
     android:id="@+id/tvName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight=".3" 
     android:text="Name"/> 

    <TextView 
     android:id="@+id/tvAddress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight=".3" 
     android:text="Address: "/> 

    <TextView 
     android:id="@+id/tvGender" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight=".3" 
     android:text="gender: "/> 
</LinearLayout> 

更新

现在我改变了布局如下,但问题是挖墙角是,这三个textviews正在出现紧抓住对方没有间距。

布局

<?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="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <TextView 
     android:id="@+id/tvName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Name: " 
     android:focusable="false"/> 

    <TextView 
     android:id="@+id/tvAddress" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Address: " 
     android:focusable="false"/> 

    <TextView 
     android:id="@+id/tvGender" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="gender: " 
     android:focusable="false"/> 
</LinearLayout> 

屏幕截图

enter image description here

+0

可以共享屏幕截图吗? – Enzokie

+0

@ Enzokie屏幕截图添加 – user2121

+0

至于'xml'布局是好的,你确定你膨胀正确的布局文件? – Pztar

回答

0

使用属性 机器人:layout_margin = “10dp”

+0

但为什么会发生尽管有每个TextView指定的权重? – user2121

+0

尝试使用重量而不是layout_weight。此外,请尝试将宽度指定为match_parent。使用这些组合。我不确定为什么你的文本视图的行为是这样的,但也许是因为宽度的组合。 –

1

添加layout_marginpadding每个TextView

+0

但为什么会发生这种情况,尽管每个TextView都有一个指定的权重? – user2121

+1

@ user2121,因为如果你将'layout_width'设置为'wrap_content',并且将你的根布局设置为'android:orientation =“horizo​​ntal”'你将拥有你想要的外观 – Pztar

0
在你的ListView XML

补充:

android:dividerHeight="8 dip" 

或任何价值你想要的。

2

您需要指定方向LinearLayoutandroid:orientation="horizontal"然后将您的权重替换为1而不是0.3,并确保您的TextView宽度为0dp而不是wrap_content

+1

这是正确的答案,除了'layout_weight'不需要是1,只要权重的总和不超过'weightSum'(它没有被指定) – Pztar

+0

请看我的更新布局..我做你建议但仍然问题perisists – user2121

+0

@ user2121请添加一个屏幕截图,让我们可以看看 – Enzokie

0

我建议从TextViews删除layout_weight,为它们定义一个特定的height并添加填充。添加页边距可能导致您的TextViews超出ListView

相关问题