[英]Navigation drawer with other component
導航抽屜如何與其他組件一起使用?
我的activity_main是
<LinearLayout
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="262dp"
android:orientation="vertical" >
<Button
android:id="@+id/button0"
android:layout_width="match_parent"
android:layout_height="85dp"
android:background="@drawable/..."
/>
...
</LinearLayout>
有了這個xml文件,我想在我的App中添加導航抽屜。
activity_main.xml
:
<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 -->
<LinearLayout
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- your buttons and whatever else is in your main layout -->
</LinearLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
要初始化抽屜,請在MainActivity.java
:
public class MainActivity extends Activity
{
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
...
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close)
{
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(yourAdapter);
mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView parent, View view, int position, long id)
{
// Deal with the selection
selectItem(position);
}
});
}
}
要處理導航抽屜的單擊事件,請參見此處 。
要收聽導航抽屜的打開和關閉事件,請參見此處 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.