2013-03-11 11 views
24

ListViewアイテムに余白を追加しようとしています。下のRelativeLayoutに余白の値を追加しようとしましたが、私が得るすべてのことは、各アイテム間に1ピクセルの線があるように見えます。リストの余白を表示します。

私が本当に好きなのは、各項目に1pxの黒い枠線と3〜5pxの余白を左上、右上、右端に丸めて表示することです。 )

どのように目標を達成できますか?今だけの余裕... ;-)

は、ここで私が持っているものです。

UPDATE:私はメインのレイアウトおよびフラグメントレイアウトを削除し、以下のXMLを更新しました。 ListViewのアイテムレイアウトを更新しましたが、これは私が今使っているものに近づいていますが、まだ完全ではありません。スクリーンショットは

リストビュー項目のレイアウトXML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="@dimen/matchMargin" 
android:paddingRight="@dimen/matchMargin" 
android:paddingTop="@dimen/matchMargin" > 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#cfcfcfcf" > 

    <include 
     android:id="@+id/matchKampstart" 
     layout="@layout/kampstart_layout" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/matchKampstart" 
     android:layout_marginTop="@dimen/belowKampstartMargin" > 

     <ImageView 
      android:id="@+id/tournamentImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/tournamentImageViewContentDescription" 
      android:gravity="left" 
      android:src="@drawable/sofabold_launcher" /> 

     <ImageView 
      android:id="@+id/homeTeamImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/homeTeamImageViewContentDescription" 
      android:src="@drawable/sofabold_launcher" /> 

     <TextView 
      android:id="@+id/homeTeam" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:text="@string/home" 
      android:textSize="14sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/dash" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:gravity="center" 
      android:text="@string/dash" 
      android:textSize="12sp" 
      android:textStyle="bold" /> 

     <ImageView 
      android:id="@+id/awayTeamImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/awayTeamImageViewContentDescription" 
      android:src="@drawable/sofabold_launcher" /> 

     <TextView 
      android:id="@+id/awayTeam" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:gravity="center" 
      android:text="@string/away" 
      android:textSize="14sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/matchKampstart" 
     android:layout_marginTop="@dimen/belowKampstartMargin" > 

     <ImageView 
      android:id="@+id/tvChannelImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="false" 
      android:contentDescription="@string/tvChannelImageViewContentDescription" 
      android:gravity="right" 
      android:src="@drawable/sofabold_launcher" /> 
    </LinearLayout> 

</RelativeLayout> 

</RelativeLayout> 

同様

を追加しましたあなたは、各項目の右と左に、非常に小さなラインに気付くでしょうここは私に次のようになります。私はまた取り除きたいと思っています。

アダプタで

enter image description here

+0

あなたはマージンやパディングをしたいですか? – Sam

+1

丸みを帯びたコーナーと境界線を作成することはそれほど難しくありませんが、この[開発者ガイド](https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)ではいくつかの詳細について説明しています。 – Sam

+0

私は余裕があります。私は私のリスト項目を分離したい。 – CJe

答えて

59

私はレイアウトの偉大ないんだけど、私は過去に気づきましたそのListView行は、多くの場合、LayoutParamsを無視します。私は、この問題が発生しない考えを持っていないか、またはそれを上書きすることができます場合、私はあなたが簡単に別のレイアウトを追加することによって、それを回避することができます知っていますか:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:background="#990000ff" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:background="#9900ff00" 
     android:paddingLeft="10dp" > 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#99ff0000" /> 
    </LinearLayout> 

</LinearLayout> 

通常することができますように一つだけの子供を持って取り外すことができますが、ことをレイアウトしますこの1つは目的を果たす参照してください。

Screen Shot

最も外側のレイアウトは青で、TextViewには赤、緑はあなたには、いくつかの余分なスペースを追加することができます余分なレイアウトです。パディング(左の緑)とマージン(右の緑がない)の違いに注目してください。あなたはマージン(android:layout_margin)を使用したいと明確に述べていますが、あなたのコードはパディング(android:padding)を使用しているので、両方を含んでいます。

+0

Thanks Sam。私はあなたが描いているように(余分なレイアウトを追加して)多かれ少なかれ実行したと思います。 AFAICSあなたは右の行と同じ問題を抱えています(おそらく同様に残っています)。どのようにそれらを取り除くための任意のアイデア? – CJe

+2

「非常に小さいライン」とは、[これらの](http://i.stack.imgur.com/KnXaV.png)を意味しますか?それらは 'android:divider'を空白または透明なイメージに変更するか、おそらく' android:dividerHeight = "0dp" 'を設定することによってそれらを削除できる仕切りです。 – Sam

+0

外側のRelativeLayoutにパディングする理由は、内側のRelativeLayoutに余裕を持たせることと同じです...そうではありませんか?私はそれを逆転しようとします。 – CJe

4

、getViewメソッドであなたの相対的なレイアウトをキャッチ()は、レイアウトのparamsを与え、

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams(); 
params.setMargins(80, 0, 0, 0); //substitute parameters for left, top, right, bottom 
YourRelativeLayoutInAdapter.setLayoutParams(params); 
+0

ありがとうございます。それは行くだろう。これをXMLで行う方法はありませんか?そしてもしそうなら、なぜですか? – CJe

+1

私はそれを正確に知っていませんが、前にxmlを使って試してみました。私も私のために働いていませんでした。だから私はアダプターに余裕を与えることでこの問題を解決しました。 – Talha

+0

動作させることができません。更新された質問。 – CJe

15

少し遅れてこれを見て、ちょうどあなたのListViewlayout_widthを追加し、中央でそれを置く、またはmarginことによってそれを動かす

android:divider="#00000000" 
android:dividerHeight="10dp" 
0

あなたのListView XML要素に次の行を追加します。 (たぶんandroid:layout_centerHorizontal="true"による。)

それは私のために素晴らしい仕事を。

0

ListViewがXMLファイルで定義されているとします。ある場合は、paddingを追加して、画面の端とListViewの間にスペースができるようにします。

例:

<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="15dp"/> 
関連する問題