2017-04-04 18 views
0

追加したいcardUseCompatPaddingを使用せずにCardViewのカード間にスペースを入れる方法はありますか?行のアンドロイドでCardViewのカード間に水平間隔を追加したいですか?

+0

あなたca n _padding_を使用します。詳細はスクリーンショットを示します。 – Piyush

+0

なぜあなたはcardUseCompatPadding ??????を使いたくないのですか? – AwaisMajeed

+0

cardUseCompatPaddingの使用について私は左/右にスワッピングしている場合は、背景がそのパディングを取らず、より多くの高さと幅が付属します – Divya

答えて

1

セットパディングので、すべての項目は、各項目

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:background="#FFF" 
android:padding="5dp" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

// here your views 

</LinearLayout> 

や、アイテム

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    recyclerView.setLayoutManager(mLayoutManager); 
    recyclerView.setNestedScrollingEnabled(false); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing); 
    recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels)); 

クラス

public class SpacesItemDecoration extends RecyclerView.ItemDecoration { 
private int space; 

public SpacesItemDecoration(int space) { 
    this.space = space; 
} 

@Override 
public void getItemOffsets(Rect outRect, View view, 
          RecyclerView parent, RecyclerView.State state) { 
    outRect.left = space; 
    outRect.right = space; 
    outRect.bottom = space; 

    // Add top margin only for the first item to avoid double space between items 
    if (parent.getChildLayoutPosition(view) == 0) { 
     outRect.top = space; 
    } else { 
     outRect.top = 0; 
    } 
} 

をsaperateするためにJavaでこのように使用との間の間隔を設定すること}

+1

ありがとう!! Javaコードは私のために働いた – Divya

+1

正しい私の答えとしてマーク@Divya –

関連する問題