我对Android开发还很陌生,我正在尝试使用4个按钮自定义导航抽屉,然后我想根据自己的外观自定义,类似于VSCO Cam,但没有标题图像,只是占据导航抽屉中所有空间的4个按钮。
我现在正在做的是使用FrameLayout为主要内容创建抽屉布局,使用LinearLayout为抽屉创建。然后我在线性布局中放置了4个按钮。
<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">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Sidemenu (Navigation Drawer) -->
<LinearLayout android:id="@+id/left_drawer"
android:layout_width="240dp"
android:orientation="vertical"
android:layout_height="match_parent"
android:gravity="left">
<Button
android:id="@+id/btn_lend_return"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/lend_return"/>
<Button
android:id="@+id/btn_inv_order"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/manage_inventory"/>
<Button
android:id="@+id/btn_inventar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/inventory"/>
<Button
android:id="@+id/btn_add_user"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/add_user"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
对于Java代码:
public class MainActivity extends Activity {
// constants for db
public static final String DATABASE_NAME = "sync_gateway";
public static final String designDocName = "sync_gateway-local";
public static final String byDateViewName = "byDate";
public static final String SYNC_URL = "http://141.45.176.157:4984/sync_gateway";
// couchbase internals
protected static Manager manager;
//private CharSequence mDrawerTitle;
private static CharSequence mTitle;
//Fragments
FragmentWelcome fragmentWelcome = new FragmentWelcome("Welcome", R.layout.fragment_welcome);
private Database database;
private LiveQuery liveQuery;
// UI
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle; //Used in the example, to be removed in final
private LinearLayout mDrawerLinear; //To use Custom Buttons, I guess we need to use ImageView instead of ListView
Button btn_lend_return;
Button btn_inv_order;
Button btn_inventar;
Button btn_add_user;
//private String[] mNavigationTitles;
final String TAG = "VerleihSystem";
FragmentLendReturn fragmentLendReturn = new FragmentLendReturn("Ausleihe/Rückgabe", R.layout.fragment_ausleihe__rueckgabe);
FragmentInvOrder fragmentInvOrder = new FragmentInvOrder("Bestand verwalten", R.layout.fragment_bestand_verwalten);
FragmentInventar fragmentInventar = new FragmentInventar("Bestand", R.layout.fragment_bestand);
FragmentAddUser fragmentAddUser = new FragmentAddUser("User hinzufuegen", R.layout.fragment_user_hinzufuegen);
public static void setDrawerTitle(String title) {
mTitle = title;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mNavigationTitles = getResources().getStringArray(R.array.nav_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
View.OnClickListener buttonHandler = new DrawerItemClickListener();
btn_lend_return = (Button) findViewById(R.id.btn_lend_return);
btn_inv_order = (Button) findViewById(R.id.btn_inv_order);
btn_inventar = (Button) findViewById(R.id.btn_inventar);
btn_add_user = (Button) findViewById(R.id.btn_add_user);
btn_lend_return.setOnClickListener(buttonHandler);
btn_inv_order.setOnClickListener(buttonHandler);
btn_inventar.setOnClickListener(buttonHandler);
btn_add_user.setOnClickListener(buttonHandler);
// set a custom shadow that overlays the main content when the drawer opens TODO
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// enable ActionBar app icon to behave as action to toggle nav drawer
// TODO: Speak with Group about this, propably needs to be removed so that no
// ActionBar App Icon exists
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//ActionBarDrawerToggle for interactions between sliding the drawer and the app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, // Host Activity(Context)
mDrawerLayout, // DrawerLayout Object
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragmentWelcome);
fragmentTransaction.commit();
}
}
//Click listener for the ListView in the Drawer
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void onButton(View v) {
//update the main content by replacing Fragments in the main View "view"
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.btn_lend_return:
fragmentTransaction.replace(R.id.content_frame, fragmentLendReturn);
break;
case R.id.btn_inv_order:
fragmentTransaction.replace(R.id.content_frame, fragmentInvOrder);
break;
case R.id.btn_inventar:
fragmentTransaction.replace(R.id.content_frame, fragmentInventar);
break;
case R.id.btn_add_user:
fragmentTransaction.replace(R.id.content_frame, fragmentAddUser);
break;
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
//update selected item and title, then close the drawer
setTitle(mTitle);
mDrawerLayout.closeDrawer(mDrawerLinear);
}
@Override
public void setTitle(CharSequence title) {
//mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
public void onBackPressed() {
super.onBackPressed();
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
* <p/>
* STRAIGHT EXAMPLE COPY, I have no Idea what this does
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
private class DrawerItemClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
onButton(v);
}
}
}
所以我要做的是使用4个按钮作为导航来更改片段,我正在尝试使用按钮而不是列表视图,因为我只使用4个导航元素,所以我可以制作它们占据导航抽屉中1/4的空间。
但是,执行此代码会给我一个错误,指出我的LinearLayout不是滑动抽屉。
java.lang.IllegalArgumentException: View android.widget.LinearLayout{b2d82ec0 V.E..... ........ 0,0-480,618 #7f08003e app:id/left_drawer} is not a sliding drawer
我只发现另一个人有同样的问题,但他们已经通过更换解决了这个问题 mDrawerLayout.closeDrawer(mDrawerLinear); 使用正确的抽屉布局,这是我已经完成的事情。
编辑:我忘记添加的是它确实开始,但线性布局分层在内容片段上方,填满整个屏幕并且有点半透明: http://imgur.com/SUDrS9I每当你点击像切换按钮或其中一个按钮一样,它就会崩溃。
有谁知道我做错了什么?如果还有另一个更好的方法,那我就是开放的! :) 提前谢谢!
答案 0 :(得分:1)
好吧,我自己弄清楚了:
导航抽屉需要layout_gravity而不是gravity,所以现在一切正常。
<!-- Sidemenu (Navigation Drawer) -->
<LinearLayout android:id="@+id/left_drawer"
android:layout_width="240dp"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_gravity="left">