gpt4 book ai didi

android - 无法将 RelativeLayout 添加到 DrawerLayout

转载 作者:行者123 更新时间:2023-11-29 17:42:41 25 4
gpt4 key购买 nike

我的主要 Activity

protected void onCreate(Bundle savedInstanceState) 
{
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

xml 是:

<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/black" >

<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/sample_content_fragment"
android:background="#242a31"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0px" >


</FrameLayout>

<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view.


-->

<RelativeLayout
android:layout_width="240dp"
android:layout_gravity="start"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/left_LinearLayout"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="wrap_content">

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />


</LinearLayout>
<ListView
android:below="@+id/left_LinearLayout"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:listSelector="@drawable/selector_bg_key"
android:background="@color/default_color"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</RelativeLayout>


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

但应用程序因错误而崩溃:

android.widget.LinearLayout$LayoutParams 无法转换为 android.support.v4.widget.DrawerLayout$LayoutParams

我需要将编辑字段添加到左侧菜单,或者我应该通过适配器添加它

最佳答案

你可能正在尝试这样的事情:

mDrawerLayout.openDrawer(mDrawerList);

但是您的 ListView 的父级不是 DrawerLayout。您现在应该使用 RelativeLayout 作为抽屉:

<RelativeLayout
android:id="@+id/left_drawer"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/left_LinearLayout"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
</LinearLayout>
<ListView
android:layout_below="@+id/left_LinearLayout"
android:id="@+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</RelativeLayout>

获取布局和列表:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawer = (RelativeLayout) findViewById(R.id.left_drawer);

然后像这样打开抽屉:

mDrawerLayout.openDrawer(mDrawer);

关于android - 无法将 RelativeLayout 添加到 DrawerLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com