我正在尝试使用support-lib actionbaractivity创建一个ViewDrawer。 我的布局看起来像
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="@+id/drawer_view"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="@layout/include_drawer" />
</android.support.v4.widget.DrawerLayout>
活动看起来像这样:
public abstract class BaseActivity extends BaseSimpleActivity {
public static final String FRAGMENT_TAG = "content";
protected DrawerLayout mDrawerLayout;
protected ActionBarDrawerToggle mDrawerToggle;
protected ListView mDrawerList;
protected ViewGroup mDrawerView;
protected View mDrawerBtnSettings;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) { ... }
protected void initViews(String title, Fragment fragment) {
setContentView(R.layout.activity_base_secondary);
getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment, FRAGMENT_TAG).commit();
initDrawer();
initActionBar();
}
protected void setTitle(String title) {
super.setTitle(title);
getSupportActionBar().setTitle(title);
}
private void initActionBar() {
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);
//actionBar.setIcon(R.drawable.ic_launcher);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.action_bar);
TextView title = (TextView) actionBar.getCustomView().findViewById(R.id.actionBarTitle);
title.setText(getTitleResId());
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.openDrawer(Gravity.START);
}
}
});
refreshActionBar();
}
public abstract String getTitleResId();
private void initDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_container);
mDrawerView = (ViewGroup) findViewById(R.id.drawer_view);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawerList.setAdapter(new DrawerListAdapter(this));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = createDrawerToggle();
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerBtnSettings = findViewById(R.id.drawer_settings);
refreshDrawer();
}
private ActionBarDrawerToggle createDrawerToggle() {
return new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
refreshActionBar();
int size = menu.size();
mMenuItemIds = new int[size];
for (int i = 0; i < size; i++) {
mMenuItemIds[i] = menu.getItem(i).getItemId();
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
protected boolean isDrawerOpen() {
return mDrawerLayout.isDrawerOpen(mDrawerView);
}
/**
* Update the title and invalidate the options menu
*/
protected void refreshActionBar() {
if (isDrawerOpen()) {
setActionBarItemsAlpha(0);
} else {
getSupportActionBar().setTitle(getTitle());
}
}
/**
* Update the selected item.
*/
protected void refreshDrawer() {
Navigation id = Navigation.getId(this);
if(id != null) {
mDrawerList.setItemChecked(id.ordinal(), true);
}
}
protected void onDrawerItemClick(int which) {
// Override if subclass needs to listen for Drawer click events
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectDrawerItem(position);
}
}
protected void selectDrawerItem(final int which) {
Navigation id = Navigation.getId(this);
if (id != null && which == id.ordinal()) {
return;
}
onDrawerItemClick(which);
mPendingLaunch = DrawerUtils.buildLaunchRunnable(this, which);
mDrawerLayout.closeDrawer(mDrawerView);
}
}
但是正如您在下面的屏幕截图中看到的那样,抽屉的图标在某些设备上呈现,而在其他设备上则不呈现。当我检查android监视器时,布局的宽度为零。关于最新情况的任何线索?
答案 0 :(得分:2)
如果您使用的是AppCompat的v21,则需要使用android.support.v7.app.ActionBarDrawerToggle而不是您正在使用的已弃用的v4版本。