-1

I 3 Sätze von Objekten aus der Datenbank zu holen versuchen, und ich mag, dass sie, wie dies in einer Art und Weise angezeigt werden:zwei Spalten einer CardView in einem Linearlayout angezeigt

aber es ist ihnen nur setzen an einem Ort statt Ausbreiten, siehe hier:

ich vermute, dass die cardviews einander überlappen und alle an einem Ort anstatt sich die zur Verfügung stehende Fläche zu bleiben.

ich Linearlayout für den Code verwenden:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:weightSum="2" 
    > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:weightSum="2"> 

     <android.support.v7.widget.CardView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="20dp" 
      android:layout_weight="1" 
      android:layout_gravity="left"> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 
       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_addimage" 
        android:id="@+id/itemImage" 
        android:scaleType="fitCenter" 
        android:padding="2dp" 
        android:adjustViewBounds="true" />' 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Item name" 
        android:paddingLeft="15dp" 
        android:paddingRight="15dp" 
        android:paddingTop="5dp" 
        android:textSize="20dp" 
        android:textStyle="bold" 
        android:id="@+id/itemName"/> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Price" 
        android:textStyle="normal" 
        android:paddingTop="3dp" 
        android:paddingLeft="15dp" 
        android:paddingRight="15dp" 
        android:paddingBottom="10dp" 
        android:id="@+id/itemPrice"/> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 
    </LinearLayout> 
</LinearLayout> 
+0

Das sieht aus wie ein einziger 'CardView'. Legen Sie es in ein anderes Layout, mit dem Sie diese "CardView" replizieren können, um mehrere Karten anzuzeigen? Ein Beispiel finden Sie [hier] (https://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465). – Cheticamp

+0

Denken Sie auch daran, Screenshots direkt in Stack Overflow hochzuladen, da diese dann auf dem Imgur Pro-Konto von Stack Overflow gehostet werden, wodurch sichergestellt ist, dass keine Bilder verloren gehen. Siehe [offizielle Ankündigung hier] (https://stackoverflow.blog/2010/08/new-image-upload-support/) und [diese Meta-Antwort] (http://meta.stackexchange.com/a/75498/310976). –

Antwort

0
I tried this code to get the cardviews 

<?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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/tools" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     app:cardBackgroundColor="#999" 
     android:layout_height="270dp" 
     android:layout_margin="5dp" 
     app:cardCornerRadius="25dp" 
     android:elevation="5dp"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
     <TextView 
      android:id="@+id/tv_title" 
      android:text="Dance" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="38dp" 
      android:maxLines="1" 
      android:layout_marginRight="10dp" 
      android:layout_marginLeft="20dp" 
      android:textColor="#FFF" 

      /> 

    <TextView 
     android:id="@+id/tv_subtitle" 
     android:text="10:30 60min" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_title" 
     android:textSize="26dp" 
     android:maxLines="1" 
     android:layout_marginTop="8dp" 
     android:textColor="#FFF" 
     android:layout_marginLeft="20dp" 
     /> 
    <TextView 
     android:id="@+id/tv_description" 
     android:text="There are many forms of dance from 
ball room tobarn dancing and disco to 
moris dancing" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_subtitle" 
     android:layout_marginLeft="20dp" 
     android:textSize="12dp" 
     android:textStyle="bold" 
     android:maxLines="4" 
     android:layout_marginTop="8dp" 
     android:textColor="#FFF" 
     /> 


    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/tv_description" 
     android:layout_marginTop="20dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/left"> 
      <TextView 
       android:id="@+id/tv_trainer_title" 
       android:text="Associated trainer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:textSize="14dp" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" 
       android:textStyle="bold" 
       android:textColor="#FFF" 
       android:layout_marginBottom="10dp" 
       android:layout_marginLeft="20dp" 
       /> 
      <ImageView 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:id="@+id/imageView2" 
       android:layout_marginLeft="20dp" 
       android:layout_below="@+id/tv_trainer_title" 
       android:layout_gravity="left" 
       android:background="@drawable/image_circle" 
       /> 
      <TextView 
       android:id="@+id/textView4" 
       android:text="TextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="12dp" 
       android:textStyle="bold" 
       android:layout_gravity="left" 
       android:layout_marginLeft="20dp" 
       /> 


     </LinearLayout> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/right"> 
      <TextView 
       android:id="@+id/tv_trainer_price" 
       android:text="£5 only" 
       android:textSize="28dp" 
       android:textAlignment="center" 
       android:gravity="center" 
       android:layout_width="116dp" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/textView3" 
       android:layout_marginTop="0dp" 
       android:layout_marginRight="12dp" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginBottom="5dp" 
       /> 

      <Button 
       android:id="@+id/btn_book_now" 
       android:background="@drawable/image_roundedcorner" 
       android:layout_marginRight="30dp" 
       android:layout_width="170dp" 
       android:layout_height="wrap_content" 
       android:text="Book now" 
       android:textStyle="normal" 

       android:textSize="18dp"/> 

     </LinearLayout> 

    </LinearLayout> 



</RelativeLayout> 


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

</LinearLayout> 
0

Sie können nicht tun, dass aus XML Seite, was Sie tun müssen, wenn Sie mit der Verwendung von gridlayoutmanager wie diese

Elemente aus Adapter laden
mLayoutManager = new GridLayoutManager(this,2); 
recyclerView.setLayoutManager(mLayoutManager); 
0

Sie können dies nicht von der XML-Seite aus tun, Sie müssen das tun, wenn Sie Artikel vom Adapter mit gridlayoutmanager wie dieser laden

In Fragmente:

GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(),2); 
recyclerView.setLayoutManager(mLayoutManager); 
Verwandte Themen