【问题标题】:Displaying two columns of a CardView in a LinearLayout在 LinearLayout 中显示 CardView 的两列
【发布时间】:2017-02-24 15:58:54
【问题描述】:

我正在尝试从我的数据库中获取 3 组对象,我想以如下方式显示它们:

但它只是将它们放在一个地方而不是分散开来,请参见此处:

我猜卡片视图是相互重叠的,并且都停留在一个地方,而不是去可用区域。

我正在为代码使用 LinearLayout:

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

【问题讨论】:

  • 这看起来像一个CardView。您是否将其放置在另一个布局中以允许您复制此CardView 以显示多张卡片?有关示例,请参见 here
  • 还可以考虑将屏幕截图直接上传到 Stack Overflow,因为它们将托管在 Stack Overflow 的 Imgur Pro 帐户上,以确保图像不会丢失。见official announcement herethis meta answer

标签: xml android-layout android-recyclerview android-linearlayout android-cardview


【解决方案1】:

您不能从 XML 端执行此操作,当您使用 gridlayoutmanager 从适配器加载项目时必须这样做

在片段中:

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

【讨论】:

    【解决方案2】:

    你不能从 xml 端做到这一点,当你使用 gridlayoutmanager 从适配器加载项目时,你必须这样做

     mLayoutManager = new GridLayoutManager(this,2);
     recyclerView.setLayoutManager(mLayoutManager);
    

    【讨论】:

      【解决方案3】:
      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-17
        • 2015-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多