gpt4 book ai didi

android - 工具栏导航按钮

转载 作者:行者123 更新时间:2023-11-29 01:35:45 25 4
gpt4 key购买 nike

这是我的工具栏: my toolbar

这是 Gmail 应用工具栏: gmail toolbar

我想要打开 Gmail 应用程序所具有的抽屉导航的按钮。

我怎样才能做到这一点?

这是我的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/abc_action_bar_default_height_material"
android:background="#EDAA00" />

这是我的应用主题:

<style name="noActionBar" parent="Theme.AppCompat.Light.NoActionBar"/>

这是我创建工具栏的代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

...

 mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
)

提前致谢!

最佳答案

我们在 onCreate 中使用这样的代码:

toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);// Supported toolbar
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); //this is the navigation drawer
content_frame=(FrameLayout)findViewById(R.id.content_frame);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open,R.string.drawer_close){
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
float moveFactor = (getActivity().findViewById(R.id.left_drawer).getWidth() * slideOffset);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
content_frame.setTranslationX(moveFactor);
}
else
{
TranslateAnimation anim = new TranslateAnimation(lastTranslate, moveFactor, 0.0f, 0.0f);
anim.setDuration(0);
anim.setFillAfter(true);
content_frame.startAnimation(anim);
lastTranslate = moveFactor;
}
super.onDrawerSlide(drawerView, slideOffset);
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
drawerLayout.post(new Runnable() {
@Override
public void run() {
actionBarDrawerToggle.syncState();
}
});

@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(drawerListview);
} else super.onBackPressed();
}

但不要忘记 v7 支持库:

import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;

在不向工具栏添加任何可绘制对象的情况下,ActionBarDrawerToggle v7 添加它并赋予它动画效果,当抽屉打开时,它会转换为后退按钮

也请参阅此链接 Drawer Arrow Drawable

关于android - 工具栏导航按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28188229/

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