2014-07-03 20 views
32

我正在玩新的CardView,但邊距似乎沒有應用。CardView layout_margin不起作用

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

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:id="@+id/card_view" 
android:layout_gravity="center" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="18dp" 
android:layout_marginLeft="18dp" 
android:layout_marginRight="18dp" 
card_view:cardCornerRadius="4dp" 
card_view:cardBackgroundColor="#FFA" 
card_view:layout_margind="4dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

<TextView 
    android:id="@+id/info_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!"/> 

</LinearLayout> 

</android.support.v7.widget.CardView> 

順便說一句,我用它在ViewPager中的片段。即使我在CardView上使用android:layout_marginLeft =「18dp」和android:layout_marginRight =「18dp」,該卡也會擴展整個屏幕寬度(match_parent)。

任何想法我可能做錯了什麼?

回答

17

我收到了同樣的問題,我找到答案在這裏... CardView layout_width="match_parent" does not match parent RecyclerView width

在膨脹的ViewPager內CardView XML的時間,通過在的ViewGroup它。這將允許inflater知道要引用哪個佈局參數。

膨脹佈局文件使用類似下面:

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_list_item, viewGroup,false); 

希望幫助

+0

在那裏我會使用這些代碼?我正在使用FragmentStatePagerAdapter。 –

+0

當你膨脹的觀點,在片段或適配器。 –

+0

這工作對我來說 – yeahdixon

30

如果使用RecyclerView添加CardView,安卓layout_margin應該足夠了。

但是,使用ListView控件添加CardView,你可以這樣做:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" > 

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="11dp" 
    android:layout_marginRight="11dp" 
    android:layout_marginTop="11dp"> 
     (other layout ...) 
</android.support.v7.widget.CardView> 

</FrameLayout> 

但它通常不是最優的。

+0

看起來像一個醜陋的黑客,但爲我工作。直到我轉換成'RecylerView'時纔會這樣做。 – KickingLettuce

+3

AbsListView.LayoutParams不會擴展ViewGroup.MarginLayoutParams。 http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html因此,邊距不適用於直接的子視圖。儘管如此,RecyclerView.LayoutParams確實擴展了MLP。 –

0

到RecyclerView或ListView添加利潤率而不是卡片視圖或行項目佈局