我想在android中创建一个包含选项卡的导航抽屉,但我无法做任何事情。我想在导航抽屉中添加标签(类别,收藏夹)。它可以吗?
(例如,ios)
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer,
R.string.app_name, R.string.app_name ){
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
// invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
//invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(getApplicationContext(), getSupportFragmentManager(), android.R.id.tabcontent );
tabHost.addTab(tabHost.newTabSpec("surec").setIndicator("Süreç listesi"), SurecListesiFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("bolum").setIndicator("Bölüm listesi"), PhotosFragment.class, null);
我无法在标签之间切换。我不能在标签内容上使用任何小部件。 (列表视图,按钮等..)
(编辑:解决方案) (linearlayout:滑块菜单布局)
linearLayout.bringToFront();
linearLayout.requestLayout();
答案 0 :(得分:1)
我昨天就做到了。
这是我用抽屉主要活动的布局:
<?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" >
<!-- Main layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_background"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_location_spinners_campaigns" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_campaignslistview" />
</LinearLayout>
<!-- Slider menu -->
<LinearLayout
android:id="@+id/preferencesDrawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/app_background"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
显示标签的方法:
FragmentTabHost tabhost;
void buildTabs() {
tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabhost.addTab(tabhost.newTabSpec("locations").setIndicator(getString(R.string.locations)), FragmentPreferencesLocations.class, null);
tabhost.addTab(tabhost.newTabSpec("categories").setIndicator(getString(R.string.categories)),FragmentPreferencesCategories.class, null);
}
除此之外,您需要使用代码来显示抽屉并自定义标签...
编辑:
我用这个方法初始化抽屉(在onCreate上调用它):
ActionBarDrawerToggle drawerToggle;
LinearLayout preferencesDrawer;
FragmentTabHost tabhost;
...
void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.app_name,
R.string.app_name) {
public void onDrawerClosed(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.app_name));
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
drawerClosed(drawerView);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.preferences));
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
// Invoked by action bar button
void clickFilterCategories() {
toggleDrawer(preferencesDrawer);
}
void toggleDrawer(View drawer) {
if (drawerLayout.isDrawerOpen(drawer)) {
closeDrawer(drawer);
} else {
openDrawer(drawer);
}
}
void openDrawer(View toOpen) {
drawerLayout.openDrawer(toOpen);
}
void closeDrawer(View toClose) {
drawerLayout.closeDrawer(toClose);
}