- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在我的 CustomDrawerLayout 中使用 ActionBarDrawerToggle。当我尝试 import import android.support.v4.app.ActionBarDrawerToggle;
时,它已被弃用。当我尝试 import import android.support.v7.app.ActionBarDrawerToggle;
时,构造函数不接受五个参数并且 ActionBar 中的图标未更改。我的问题是我应该使用已弃用的 v4 还是带有四个参数的 v7?
这里。
v4
/**v4 works with 5 arguments in constructor and change icon ActionBar but it's deprecated*/
import android.support.v4.app.ActionBarDrawerToggle;
private ActionBarDrawerToggle tg;
//ActionBarDrawerToggle deprecated
tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
v7
/**v7 works with 4 arguments in constructor and not change icon of ActionBar*/
import android.support.v7.app.ActionBarDrawerToggle;
private ActionBarDrawerToggle tg;
tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
自定义抽屉布局
public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private List<ItensListView> fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
init();
if(savedInstanceState == null){
selectedItem(0);
}
}
private void init(){
//actionbar
onConfigActionBar();
//listview
configItensListView();
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.dl);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}
/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}
/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}
/** necessario */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
FragmentTransaction ft;
Fragment frag;
if(item.getItemId() == R.id.action_chat){
frag = new HelloBubblesActivity();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag, "HelloBubblesActivity");
ft.addToBackStack("back");
ft.commit();
}
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}
/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(lv);
menu.findItem(R.id.action_settings).setVisible(!status);
return super.onPrepareOptionsMenu(menu);
}
最佳答案
在 v7 构造函数中,您应该将其作为参数传递:
new ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes)
虽然v4构造函数是
new ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes)
我认为在您的 v7 构造函数中,您将 drawerImageRes 作为第三个参数 (ic_launcher)。
试试这个:
new ActionBarDrawerToggle(this, dl, R.string.drawer_open, R.string.drawer_close)
关于android - ActionBarDrawerToggle v7 还是 v4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27532935/
Android Studio 1.1 Beta 4 你好, 我期待下面的一些源代码,但我无法理解其背后的原因。我能理解这部分 ActionBarDrawerToggle mActionBarDraw
如何使用 AppCompat V7 在 android 中更改 ActionBarDrawerToggle 的图标?另外,我们如何为 ActionBarDrawerToggle 图标指定参数和填充等?
我正在尝试实现抽屉式导航,但出现此错误 ActionBarDrawerToggle 不能应用于 android.support.v7.widget.Toolbar mDrawerToggle = n
我正在使用 ActionBarDrawerToggle(抽屉导航),但我无法将其设置在右上角(波斯语)。 这是我的 activity_main.xml 代码:
我有简单的代码来创建简单的抽屉导航,但是当我为 ActionBarDrawerToggle 声明参数时,它说无法应用可绘制图标... Gradle 按摩构建 Error:(36, 26) error:
如何以编程方式在汉堡包图标和后退图标之间切换? 使用下面的代码我可以将汉堡包图标改回原来的样子,但我想把它改成动画 actionBarDrawerToggle.setDrawerIndicatorEn
我正在使用 Google 设计支持库和 DrawerLayout。 设置: final DrawerLayout drawerLayout = (DrawerLayout) findViewById(
如何在右上角设置 ActionBarDrawerToggle?因为我设置了 ListView 重力 android:layout_gravity="end" 所以我希望 ActionBarDrawer
我试图在我的应用中实现 android.support.v4.app.ActionBarDrawerToggle;因为这个类被弃用了 This class is deprecated. Please
我正在尝试在我的应用程序中添加抽屉导航。一切正常但现在我仍然看到箭头图标,尽管我用 Android 的 ic_drawer 替换了它?这是我的代码: private ActionBarDrawerTo
当我启动应用程序时,引用字符串值时出现一些问题,它们是navigation_drawer_open和navigation_drawer_close。此外,问题出现在 ActionBarDrawerTo
我正在尝试为我的 MainScreen Activity 设置 Navigation Drawer 监听器。当我设置好所有内容后,当我尝试使用 ActionBarDrawerToggle 时我的应用程
我在我的 oncreate 函数中使用了这段代码,通过按应用程序图标打开抽屉导航。 ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerT
我已经通过 java 创建了我的 Activity XML,还放置了抽屉导航。 protected void onCreate(Bundle savedInstanceState) { sup
我正在使用“android.support.v7.app.ActionBarDrawerToggle”,我需要为切换使用自定义图标,尽管我按如下方式初始化它,但我仍然获得默认的切换图标: mNavDr
有谁知道在通过资源 ID 在构造函数中设置后获取用于 ActionBarDrawerToggle 的可绘制对象的方法吗?我正在使用抽屉导航,我想以编程方式将滤色器应用于图标,但我不知道如何将其作为可绘
更新:我从谷歌的 github ( https://github.com/google/material-design-icons/blob/master/navigation/drawable-xx
当我点击主页图标时,抽屉打开,然后立即将我带回之前的 Activity 。我相信 setDisplayHomeAsUpEnabled(true) 会干扰保持打开状态的操作栏抽屉。 代码中是否有任何内容
我想手动将抽屉图标从汉堡变成箭头,反之亦然,不仅是在拖动抽屉时,这可能吗?我正在使用支持库 appcompat-v7:21。 我也找不到 android.support.v7.app.ActionBa
我是 Android 的新手,在我的应用程序中,我正在使用 DrawerLayout,它是好的,很好。但我想更改 ActionBarDrawerToggle 图标。 我们如何改变它? 我尝试了很多但我
我是一名优秀的程序员,十分优秀!