2014-07-03 15 views
32

Ich spiele mit dem neuen CardView, aber die Ränder scheinen nicht zuzutreffen.CardView layout_margin funktioniert nicht

<?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> 

Übrigens benutze ich es in einem Fragment, das in einem ViewPager ist. Die Karte erstreckt sich über die gesamte Bildschirmbreite (match_parent), obwohl ich android: layout_marginLeft = "18dp" und android: layout_marginRight = "18dp" im CardView verwende.

Irgendwelche Ideen, was ich falsch machen könnte?

Antwort

17

ich vor die gleichen Probleme hatte, fand ich Antwort hier ... CardView layout_width="match_parent" does not match parent RecyclerView width

Zu der Zeit in Ihrem ViewPager den CardView xml aufzublasen, die Viewgroup in ihm passieren. Auf diese Weise kann inflater wissen, auf welche Layout-Parameter verwiesen werden soll.

Um die Layout-Datei Verwendung so etwas wie unten aufblasen:

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

Hoffnung, die

+0

Wo würde ich diesen Code verwenden? Ich benutze einen FragmentStatePagerAdapter. –

+0

zu dem Zeitpunkt, an dem Sie die Ansicht aufblasen, entweder im Fragment oder im Adapter. –

+0

das funktionierte für mich – yeahdixon

30

hilft Wenn Sie RecyclerView verwenden CardView, android hinzuzufügen: layout_margin sollte ausreichend sein.

Aber Listview mit CardView hinzufügen, könnten Sie dies tun:

<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> 

Aber es ist in der Regel nicht als optimal.

+0

Scheint wie ein hässlicher Hack, aber arbeitete für mich. Werde dies tun, bis ich zu 'RecylerView' konvertiert habe. – KickingLettuce

+3

AbsListView.LayoutParams erweitert ViewGroup.MarginLayoutParams nicht. http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html Daher funktionieren die Ränder nicht für unmittelbar untergeordnete Ansichten. RecyclerView.LayoutParams erweitert MLP jedoch, zum Glück. –

0

hinzufügen Margen RecyclerView oder Listview stattdessen Karte anzeigen oder Row Artikel-Layout