带回收视图和卡片视图的材料导航抽屉

时间:2015-05-29 09:01:48

标签: android android-studio material-design

我尝试使用( android4devs )下面的素材导航抽屉:

http://www.android4devs.com/2014/12/how-to-make-material-design-navigation-drawer.html

所以,这是我的主要活动:

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

<android.support.v4.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp">


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


        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar">
        </include>

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

    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"

        android:background="#ffffff"
        android:scrollbars="vertical">

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


</android.support.v4.widget.DrawerLayout>

我需要像下面的教程那样做:

主要活动上有类似的事情: https://farm8.staticflickr.com/7506/15428801864_5b4a709e38_b.jpg

我尝试了这个和其他方法但我无法在主要活动上准确显示cardview:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp">


    <android.support.v7.widget.CardView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        card_view:contentPadding="16dp"
        card_view:cardElevation="2dp"
        card_view:cardCornerRadius="5dp">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <include
                android:id="@+id/tool_bar"
                layout="@layout/tool_bar">
            </include>

            <TextView
                style="@style/Base.TextAppearance.AppCompat.Headline"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Title" />

            <TextView
                style="@style/Base.TextAppearance.AppCompat.Body1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Content here" />
        </LinearLayout>
    </android.support.v7.widget.CardView>








    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ffffff"
        android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>


</android.support.v4.widget.DrawerLayout>
我认为,之间存在问题

添加cardview之前:

http://i.stack.imgur.com/1tRXy.png

结果后的图片(添加了卡片视图):

http://i.stack.imgur.com/mAHtn.png

并且我的android工作室预览有问题,所以我看不到这一点,但是当我需要看到结果时,我不得不在模拟器上运行它...

关于预览和错误的问题:

android.support.v7.internal.widget.ActionBarOverlayLayout could not be Found 以及这项主要活动:

The following classes could not be found:
- android.support.v4.widget.DrawerLayout (Fix Build Path, Create Class)
- android.support.v7.widget.CardView (Fix Build Path, Create Class)
 Tip: Try to build the project.

Build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.0"

    defaultConfig {
        applicationId "com.client.test"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:recyclerview-v7:22.0.0'
compile 'com.android.support:cardview-v7:22.0.0'
compile 'de.hdodenhof:circleimageview:1.2.1'
}

顺便说一下,

如何在MainActivity上添加此卡片视图?

干杯!

1 个答案:

答案 0 :(得分:0)

尝试将您的布局文件更改为此,看看您是否发现任何差异。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar">
        </include>

        <android.support.v7.widget.CardView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         card_view:contentPadding="16dp"
         card_view:cardElevation="2dp"
         card_view:cardCornerRadius="5dp">

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Headline"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Title" />

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Body1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Content here" />
    </android.support.v7.widget.CardView>
    </LinearLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/RecyclerView"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:background="#ffffff"
    android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>

</android.support.v4.widget.DrawerLayout>