gpt4 book ai didi

java - 抽屉导航中的 LinearLayout

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:39 27 4
gpt4 key购买 nike

我想将底部 ListView 的 TextView 放在抽屉导航中。抛出 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

主.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"
>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="0dp"
android:paddingBottom="4dp"
/>

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

<ListView
android:id="@+id/left_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:dividerHeight="0.1dp"
android:background="#111"
android:divider="#FFF"
/>

<TextView
android:id="@+id/joke_text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:lineSpacingExtra="5sp"
android:text="AAAAAA"
/>
</LinearLayout>

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

和java代码:

// Getting reference to the DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);

leftMenuItems = getResources().getStringArray(R.array.leftMenuItems);

mDrawerList = (ListView) findViewById(R.id.left_menu);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

// Generate title
String[] title = new String[] { "item1", "item2", "item4", "item4" };

// Generate icon
int[] icon = new int[] { R.drawable.abc_ic_clear, R.drawable.abc_ic_clear,
R.drawable.abc_ic_clear, R.drawable.abc_ic_clear };


MenuListAdapter adapter = new MenuListAdapter(this, title, icon);

// Setting the adapter on mDrawerList
mDrawerList.setAdapter(adapter);

// Getting reference to the ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {

public void onDrawerOpened(View drawerView) {
vstitle.setText("Menu");
invalidateOptionsMenu();
}

public void onDrawerClosed(View view) {
vstitle.setText("App");
invalidateOptionsMenu();
}


};

// Setting DrawerToggle on DrawerLayout
mDrawerLayout.setDrawerListener(mDrawerToggle);

我找到了一些相关主题,但对我没有帮助

  1. How to put list items at the bottom of list view in Navigation Drawer like Foursquare
  2. Adding a linear layout to Navigation drawer (ends up crashing with a ClassCastException)

最佳答案

万岁!我在我的代码中发现了问题。在函数 selectItem 中需要将 mDrawerLayout.closeDrawer(mDrawerList); 更改为 mDrawerLayout.closeDrawer(mDrawerLinear);现在它完美地工作了!感谢大家

关于java - 抽屉导航中的 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21810727/

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