【问题标题】:Material Navigation Drawer with recycle view and Card view带有 recyclerview 和 Cardview 的材质导航抽屉
【发布时间】:2015-05-29 09:01:47
【问题描述】:

我正在尝试使用来自 (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

我尝试了这种方法和其他方法,但我无法在主 Activity 上准确显示卡片视图:

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

我认为,两者之间存在问题

添加卡片视图之前:

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

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

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

我的 android studio 预览有问题,所以我看不到这个,但是当我需要查看结果时,我必须在模拟器上运行它...

关于预览和错误的问题:

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.

构建.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 上添加此卡片视图?

干杯!

【问题讨论】:

  • 你真的运行了这个应用程序吗?你这样做的时候有没有报错?不用担心预览。 Android Studio 仍未正确配置以在其预览窗口中显示 RecyclerView 和 CardView 等内容。
  • 您的 CardView 显示正常。您可以看到 CardView 内的文本(即此处的标题和内容)正在正确显示。我不明白你的问题是什么?
  • 主代码:http://i.stack.imgur.com/1tRXy.png在导航抽屉上没有问题。-第二代码(带卡片视图):http://i.stack.imgur.com/mAHtn.png现在,最好做出决定!
  • 您的导航抽屉布局在哪里?你从左边拉的那个?
  • 是的。你可以在本教程中看到:www.android4devs.com/2014/12/how-to-make-material-design-navigation-drawer.html

标签: android android-studio material-design


【解决方案1】:

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

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

【讨论】:

  • 我刚刚编辑了显示 DrawerLayout 结尾的代码。谢谢 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
相关资源
最近更新 更多