我正在使用developer.android.com指南来构建应用。当我在Android Studio中创建一个新项目时,我选择了“导航:导航抽屉”。我在互联网上搜索了我的问题的答案,但我找不到任何有用的。对此我很抱歉,我是编程新手。
http://developer.android.com/design/patterns/navigation-drawer.html http://developer.android.com/training/implementing-navigation/nav-drawer.html
这就是我希望布局如下:
title_section * not section_title;)
答案 0 :(得分:23)
导航抽屉现在是一种全新的趋势设计。我们使用两种布局:主要内容布局和抽屉列表布局,同时为导航抽屉活动设计xml.layout(布局)。我在这里回答你所有愚蠢的问题。
如何让我的应用在主视图中打开新片段 点击导航栏?
只需在抽屉列表项上添加clicklistener,然后根据单击列表项的位置替换主要内容中的片段。
示例代码:
// The click listener for ListView in the navigation drawer
@SuppressWarnings("unused")
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
Fragment newFragment;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (position) {
case 0:
newFragment = new f1();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 1:
newFragment = new f2();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 2:
newFragment = new f3();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 3:
newFragment = new f4();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
}
//DrawerList.setItemChecked(position, true);
setTitle(ListTitles[position]);
DrawerLayout.closeDrawer(DrawerList);
}
这里f1,f2。 f3和f4是不同的片段,每个片段都有自己的布局。你必须通过继承片段类为它们创建单独的java类。
点击导航栏中是否可以打开带有标签的多个可滑动片段?
为了在片段中实现制表符,您可以在该特定片段中使用tabhost。 假设您要在片段f_main中添加制表符。
F_main.xml的布局
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
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="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
然后使用相应的布局和java类创建其他片段f_tab1和f_tab2。 两个标签片段的布局可以相同或不同。在这里,我将它们视为相同或共同的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/google_map"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="MAP"/>
</LinearLayout>
F_tab1.java片段的代码
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class F_tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.friends_list, container,false);
return view;
}
}
另一个片段的代码。即F_tab2.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class F_tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.friends_list, container,false);
return view;
}
}
现在只需在前面提到的抽屉列表中使用clicklistener,就可以在抽屉列表中的项目点击上加载F_main,这将进一步加载主内容视图中F_main fragmnet中的标签。
如何使“标题”可扩展/可折叠?
嗯,我不知道NV抽屉是否提供此功能。但它提供了一个功能,可根据所选的抽屉项目或加载的主要内容片段切换操作栏标题。
如导航抽屉设计指南中所述,您应该在抽屉可见时修改操作栏的内容,例如更改标题并删除与主要内容相关的操作项。以下代码显示了如何通过使用ActionBarDrawerToggle类的实例覆盖DrawerLayout.DrawerListener回调方法,如下所示
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
}