Android 如何显示CardView的多列?
我希望实现这样的布局,可以动态显示相册,并根据方向(垂直或水平)更改列数。以下是我努力实现的一个例子: 现在我的代码看起来像这样Android 如何显示CardView的多列?,android,android-layout,android-studio,Android,Android Layout,Android Studio,我希望实现这样的布局,可以动态显示相册,并根据方向(垂直或水平)更改列数。以下是我努力实现的一个例子: 现在我的代码看起来像这样 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" andr
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:layout_weight="1"
android:layout_margin="8dp"
app:cardCornerRadius="0dp"
app:elevation="5dp">
<ImageView
android:id="@+id/avatar"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="top|center"
android:scaleType="centerCrop"
android:src="@drawable/wp" />
<TextView
android:id="@+id/album_name"
android:layout_width="match_parent"
android:layout_height="25dp"
android:alpha="0.85"
android:background="@color/light"
android:layout_above="@+id/detail"
android:layout_below="@+id/avatar"
android:gravity="bottom|center"
android:layout_alignParentBottom="true"
android:text="Test"
android:textColor="@color/cardview_dark_background" />
<TextView
android:id="@+id/detail"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_gravity="bottom|center"
android:alpha="0.85"
android:background="@color/light"
android:gravity="bottom|center"
android:layout_alignParentBottom="true"
android:layout_below="@+id/album_name"
android:text="Test"
android:textColor="@color/dim_foreground_disabled_material_dark" />
</android.support.v7.widget.CardView>
</LinearLayout>
这是一个好主意。在GridView
内部,您可以选择设置CardView
。您可以设置要垂直或水平显示的卡数
布局示例:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
关于完整示例,您可以看到官方示例我尝试了您的建议,但现在它显示以下消息:找不到以下类:android.v7.widget.cardwiew
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new YourCustomAlbumAdapter(this));