【问题标题】:Is it possible to use something other than a listview as sliding drawer in drawerlayout是否可以在抽屉布局中使用列表视图以外的东西作为滑动抽屉
【发布时间】:2013-05-24 15:30:39
【问题描述】:

例如,我希望从屏幕左侧滑动LinearLayoutRelativeLayout,而不是单独的ListView

我尝试将 à LinearLayoutandroid:layout_gravity="start" 一起使用,但在运行时出现此错误:

ClassCastException: android.widget.LinearLayout$LayoutParams cannot 
be cast to android.support.v4.widget.DrawerLayout$LayoutParams

这是布局文件:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout 
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <ImageView 
            android:id="@+id/ivwLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/video_icon"
        />

        <ListView 
            android:id="@+id/left_drawer"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@android:color/white"
        />

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

谢谢

【问题讨论】:

  • 你是如何在代码中引用你的 DrawerLayout 的?你能不能把那个贴出来。
  • 对于遇到android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams 问题的其他人:this question 直接回答。

标签: android drawerlayout


【解决方案1】:

如果您同时移动 android:id="@+id/left_drawer"(或创建新 id)并设置重力,这将起作用。

id move(或新的)是正确的,因此您在其上调用closeDrawer() 而不是子视图。

但最重要的是,DrawerLayout 要求该元素上设置一个 android:layout_gravity,正如您所提到的。

最后,您需要在具有所需重力的基础视图上调用 close closeDrawer()

例子:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout 
        android:id="@+id/left_drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <ImageView 
            android:id="@+id/ivwLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/video_icon" />

        <ListView 
            android:id="@+id/left_drawer_child"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@android:color/white" />

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

在代码中:

DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout)
LinearLayout mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
ListView mDrawerListChild = (ListView) findViewById(R.id.left_drawer_child);

...

mDrawer.closeDrawer(mDrawerLinear);

(这基本上是@Karakuri 发布的内容,但有更完整的解释和示例。)

【讨论】:

  • android:layout_gravity 解决了我的java.lang.IllegalArgumentException: View android.widget.LinearLayout@425d0d30 is not a drawer 错误,以防万一有人遇到同样的错误...
  • 同样,我改变了“mDrawer.closeDrawer”并将“android:layout_gravity="start"”移动到线性布局。谢谢:)
  • @OmarBizreh +1 获得正确评论,为我节省了很多搜索时间!
  • 一直在扯我的头发,重力成功了!谢谢!
  • 我很痛苦,我的错误是把android:layout_gravity 误认为android:gravity 谢谢!
【解决方案2】:

是的,任何视图都可以作为抽屉布局的滑动部分。我更喜欢将 FrameLayout 声明为抽屉并用我的片段替换它,它运行得很好。

您遇到的错误可能是由于您的实现的 Java 部分中的其他原因造成的。

【讨论】:

  • 感谢您的回复,但是,如果我删除了线性布局和图像视图以单独使用列表视图,我没有错误并且它运行良好。所以我会惊讶于java代码中的错误。
  • 介意发布代码和 logcat 吗?否则想不出可能出错的原因。
  • 最后你是对的,错误在java中。我不得不改变: mDrawerLayout.closeDrawer(mDrawerList);对于 mDrawerLayout.closeDrawer(mDrawerView);在 mDrawerView 是线性输出而不是列表的代码中。非常感谢您的帮助
  • 我有这个问题,我的意思是我有框架布局和一个片段,但后来我收到一个错误,说框架布局不是滑动抽屉,我是什么东西失踪?请协助@SamarthJain
  • 哦,好吧,我在下面的答案的 cmets 中得到了它,layout_gravity="start" 是我所缺少的!
【解决方案3】:

确保为isDrawerOpen, closeDrawer 等方法传递正确的对象(您的LinearLayout 称为mDrawerLinear)。这一行解决了我的ClassCastException

boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLinear);

【讨论】:

    【解决方案4】:

    尝试将 android:id="@+id/left_drawer" 移动到 LinearLayout 而不是 ListView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多