2016-09-23 19 views
0

以下のレイアウトは、ListViewの1つのアイテムのモデルです。ご覧のように、3つのTextViewは、それぞれが行全体のスペースのうち.3を占めているので、それぞれから分離されています。リストビュー内のアイテム間にスペースを追加する方法

問題は、ListViewにアイテムを追加すると、3つのTextViewがちょうど互いにリンクされていることがわかりました。

Adam USA M 

私はそれぞれのTextViewを分離するいくつかのスペースで画面上にその行を見ることを期待が、何が起こることは私のような何かを得ることである:例えば、次のものが含まれ、私はListViewに項目を追加したいのは、想定してみましょう次のようになります。

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> 

更新:今すぐ

は、私は次のようにレイアウトを変更しましたが、問題は3 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の代わりにweightを使用してみてください。また、widthをmatch_parentとして指定してみてください。これらの組み合わせを使用してください。あなたのテキストビューがこのように動作している理由はわかりませんが、多分幅の組み合わせが原因です。 –

1

layout_marginまたはを追加します。各TextViewへの。あなたのListView XMLで

+0

しかし、TextViewごとに指定された重みがあるにもかかわらず、なぜそれが起こっていますか? – user2121

+1

@ user2121 'layout_width'を' wrap_content'に '0dp'に設定してルートレイアウトを' android:orientation = "horizo​​ntal" 'に設定しているので、あなたはあなたの望む外観を持っているので、 – Pztar

0

追加:

android:dividerHeight="8 dip" 

またはあなたが望む任意の値を。

2

LinearLayoutなどの向きを指定する必要があります。 android:orientation="horizontal"あなたのウエイトを0.3の代わりに1に置き換え、TextViewの幅が0dpではなく、wrap_contentであることを確認してください。

+1

これは、 'layout_weight'が1である必要はないという点を除いて正解です。重みの総和がweightSum(指定されていない)を超えない限り、等しい重みを持つものであれば何でも構いません。 – Pztar

+0

私の更新レイアウト..私は示唆したようにしましたが、まだ問題が解決されました – user2121

+0

@ user2121 plsあなたが見ることができるようにスクリーンショットを追加します – Enzokie

0

layout_weightTextViewsから削除し、特定のheightを定義してパディングを追加することをお勧めします。余白を追加するとTextViewsListViewから外れることがあります。

関連する問題