导航抽屉不显示在主页中
Navigation drawer doesn't display in Home page
我正在使用棒棒糖(api 21 级)。我已经实现了导航
我 application.Everything 的抽屉正好是 fine.But 我没看到
输出末尾的导航抽屉。
我在 runtime.My 没有收到任何错误,唯一的问题是,我不知道
为什么我最后没有得到导航抽屉
下面贴出相关代码。
ChapterActivity.java:
public class ChapterActivity extends ActionBarActivity implements OnItemClickListener {
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chapter);
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
initialize();
listChapter.setOnItemClickListener(this);
mDrawerList.setOnItemClickListener(this);
}
private void initialize() {
......
actionBar();
}
public void actionBar() {
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#FFFFFF")));
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
}
public class getChapter extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
chapterList = DatabaseQueryHelper.getInstance().getChapters();
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
ac = new AdapterChapter(ChapterActivity.this, chapterList); // AdapterChapter
listChapter.setAdapter(ac);
mDrawerList.setAdapter(ac);
}
}
}
activity_chapter.xml:
<?xml version="1.0" encoding="utf-8"?>
<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
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textviewHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:fontFamily="fonts/Dosis.otf"
android:text="@string/heading_chapter"
android:textSize="25sp"
android:visibility="gone" />
<ListView
android:id="@+id/listviewChapters"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/imageviewBanner"
android:layout_below="@+id/textviewHeading"
android:layout_marginTop="5dp" >
</ListView>
<ImageView
android:id="@+id/imageviewBanner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/banner"
android:visibility="invisible" />
</RelativeLayout>
</FrameLayout>
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
styles.xml:
<resources>
<style name="AppBaseTheme" parent="@style/Theme.AppCompat"></style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
终于没看到家里的导航抽屉page.Anyone可以帮我this.Where我有wrong.Thank你
您似乎在使用 android.support.v4.app.ActionBarDrawerToggle
。 class 已弃用,您应该使用 android.support.v7.app.ActionBarDrawerToggle
,它也有不同的 constructor.
public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes)
public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)
第一个联系你实际行动吧。您只需要删除对 ic_launcher
的引用,并在导入中将 v4.app.ActionBarDrawerToggle
切换为 v7.app.ActionBarDrawerToggle
。
第二个与 Toolbar
视图有关,我认为您没有实现。
我看到的第二个问题是覆盖 onDrawerOpened()
和 onDrawerClosed()
方法时缺少调用。你应该总是调用 super class:
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
// other stuff
}
onDrawerOpened()
也是如此。
我正在使用棒棒糖(api 21 级)。我已经实现了导航 我 application.Everything 的抽屉正好是 fine.But 我没看到 输出末尾的导航抽屉。
我在 runtime.My 没有收到任何错误,唯一的问题是,我不知道 为什么我最后没有得到导航抽屉
下面贴出相关代码。
ChapterActivity.java:
public class ChapterActivity extends ActionBarActivity implements OnItemClickListener {
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chapter);
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
initialize();
listChapter.setOnItemClickListener(this);
mDrawerList.setOnItemClickListener(this);
}
private void initialize() {
......
actionBar();
}
public void actionBar() {
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#FFFFFF")));
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
}
public class getChapter extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
chapterList = DatabaseQueryHelper.getInstance().getChapters();
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
ac = new AdapterChapter(ChapterActivity.this, chapterList); // AdapterChapter
listChapter.setAdapter(ac);
mDrawerList.setAdapter(ac);
}
}
}
activity_chapter.xml:
<?xml version="1.0" encoding="utf-8"?>
<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
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textviewHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:fontFamily="fonts/Dosis.otf"
android:text="@string/heading_chapter"
android:textSize="25sp"
android:visibility="gone" />
<ListView
android:id="@+id/listviewChapters"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/imageviewBanner"
android:layout_below="@+id/textviewHeading"
android:layout_marginTop="5dp" >
</ListView>
<ImageView
android:id="@+id/imageviewBanner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/banner"
android:visibility="invisible" />
</RelativeLayout>
</FrameLayout>
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
styles.xml:
<resources>
<style name="AppBaseTheme" parent="@style/Theme.AppCompat"></style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
终于没看到家里的导航抽屉page.Anyone可以帮我this.Where我有wrong.Thank你
您似乎在使用 android.support.v4.app.ActionBarDrawerToggle
。 class 已弃用,您应该使用 android.support.v7.app.ActionBarDrawerToggle
,它也有不同的 constructor.
public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes)
public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)
第一个联系你实际行动吧。您只需要删除对 ic_launcher
的引用,并在导入中将 v4.app.ActionBarDrawerToggle
切换为 v7.app.ActionBarDrawerToggle
。
第二个与 Toolbar
视图有关,我认为您没有实现。
我看到的第二个问题是覆盖 onDrawerOpened()
和 onDrawerClosed()
方法时缺少调用。你应该总是调用 super class:
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
// other stuff
}
onDrawerOpened()
也是如此。