编辑:所以菜单存在,但我只能通过滑动菜单来实现,左上角的按钮不会切换抽屉打开。
我正在尝试实现一个导航菜单,但不是显示打开我的菜单的汉堡按钮,我留下了一个无效的后退按钮。我假设它与谷歌导航按钮教程略有不同,因为我使用工具栏作为我的操作栏。
MainActivity.java:
private CharSequence mTitle;
private FragmentTabHost mTabHost;
private Toolbar toolbar;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
FrameLayout frameLayout;
DrawerLayout Drawer; // Declaring DrawerLayout
ActionBarDrawerToggle mDrawerToggle; // Declaring Action Bar Drawer Toggle
String TITLES[];
int ICONS[] = {R.drawable.drawer_back, R.drawable.drawer_settings,R.drawable.drawer_notifications, R.drawable.drawer_feedback};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TITLES = getResources().getStringArray(R.array.menu_array);
mTitle = getTitle();
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<>(this,
R.layout.item_row, TITLES));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,R.string.openDrawer,R.string.closeDrawer){
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
}
main_activity.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
></include>
<include
android:id="@+id/gm_header"
layout="@layout/gm_header"
></include>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<android.support.v4.app.FragmentTabHost
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:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<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"/>
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:background="@color/ColorPrimaryDark"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</FrameLayout>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:1)
首先,声明了两个DrawerLayouts,一个名为 Drawer ,一个名为 mDrawerLayout 。我将使用 mDrawerLayout 。
在Drawer Toggle构造函数中,您可以通过使用def update
respond_to do |format|
if @user.update_with_password(user_params)
sign_in @user, :bypass => true
format.html { redirect_to user_path(@user) }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
然后添加另外几行
如果您希望在抽屉打开和关闭时图标在汉堡包和箭头之间切换,请使用此监听器:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer){
// rest of code
调用syncState将按钮和抽屉置于相同的状态(打开或关闭):
mDrawerLayout.setDrawerListener(mDrawerToggle);
答案 1 :(得分:0)
你可以做到正确的&#34;回来&#34;通过处理菜单项的单击事件来导航。您在Android中谈论的那个被称为向上按钮,它被称为home
菜单项。将此代码添加到Activity
,您就可以从当前活动返回到上一个活动:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
现在有更好的方法来处理UP导航而不是这个。这种方法只会杀死您看到的当前Activity
,因为您希望将其称为后导航。
请参阅Android文档中的this section以了解
之间的区别备份:
和Up:
了解差异后,您可以查看this section以了解应如何处理正确的向上导航。
干杯!