[英]Open Navigation Drawer onCreate
I'm currently trying to implement a Navigation Drawer into my Application. 我目前正在尝试在我的应用程序中实现导航抽屉。 For that i want the Drawer to be opened when the activity is started.
为此,我希望在活动开始时打开抽屉。 This is my onCreate:
这是我的onCreate:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
isInLandscapeMode = getResources().getBoolean(R.bool.isInLandscape);
if (!isInLandscapeMode)
{
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.date, R.string.applicationName)
{
/**
* Called when a drawer has settled in a completely closed
* state.
*/
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
drawerView.bringToFront();
drawerView.requestLayout();
}
};
// Set the drawer toggle as the DrawerListener
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
drawerLayout.openDrawer(Gravity.START);
}
The Drawer is opening as it should but if i try to click one of my list entries it closes. 抽屉正在打开,但是如果我尝试单击列表项之一,它将关闭。 If I open it manually afterwards everything is working perfectly.
如果之后手动打开它,则一切运行正常。 Do I have to set the focus on my drawerLayout somehow?
我是否必须以某种方式将焦点放在我的drawerLayout上? I have tried several things but nothing seems to work.
我已经尝试了几件事,但似乎没有任何效果。
UPDATE UPDATE
Finally found my error: 终于发现了我的错误:
I had the following code in my layout xml file: 我的布局xml文件中包含以下代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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" >
<LinearLayout
android:id="@+id/list_container"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f1f1f1"
android:orientation="horizontal" >
</LinearLayout>
<FrameLayout
android:id="@+id/detail_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
The main content view(detail_container) must be the first child in the navigation drawer thought. 主要内容视图(detail_container)必须是导航抽屉中的第一个子视图。 So i changed it to this:
所以我将其更改为:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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
android:id="@+id/detail_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</FrameLayout>
<LinearLayout
android:id="@+id/list_container"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f1f1f1"
android:orientation="horizontal" >
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
And it worked. 而且有效。
使用抽屉.openDrawer(Gravity.LEFT);
找到抽屉布局和数据(列表或可扩展列表)后,然后放入
drawerLayout.openDrawer("Name of Data Contained By Drawer");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.