用Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效果
16lz
2021-01-26
首先上效果图:
// 这两句显示左边的三条杠,如果要变为白色在toolbar的布局文件里添加这两句:
// android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
// app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
下面就是Drawer的实现了。 先贴一下toolbar的布局,因为它适合drawer联动的,等会要用到它,至于为什么单独放一个文件中,说是这样可以提高复用率。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
android.support.v7.widget.Toolbar>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
LinearLayout>
<LinearLayout
android:id="@+id/ll_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white">
<ListView
android:id="@+id/lv_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:divider="@null"
/>
LinearLayout>
android.support.v4.widget.DrawerLayout>
android:layout_gravity="start"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout_toolbar"/>
<include layout="@layout/layout_drawer"/>
LinearLayout>
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
mToggle.syncState();
mDrawerLayout.addDrawerListener(mToggle);
下面说一下用Fragment实现点击侧滑菜单项变换页面的效果。 首先定义三个布局,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="news"
android:gravity="center"
android:textSize="29sp"
/>
LinearLayout>
public class NewsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_news, container, false);
}
}
// 主页面默认添加NewsFragment
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.ll_content, new NewsFragment()).commit();
lv_drawer = (ListView) findViewById(R.id.lv_drawer);
mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
lv_drawer.setAdapter(mAdapter);
ll_drawer = (LinearLayout) findViewById(R.id.ll_drawer);
lv_drawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switchFragment(position);
}
});
// 根据所点列表项的下标,切换fragment
@Override
public void switchFragment(int fragmentId) {
mDrawerLayout.closeDrawer(ll_drawer);
if(currentFragmentId == fragmentId)
return;
currentFragmentId = fragmentId;
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (fragmentId)
{
case 0:
fragmentTransaction.replace(R.id.ll_content, new NewsFragment());
toolbar.setTitle("新闻资讯");
break;
case 1:
fragmentTransaction.replace(R.id.ll_content, new VideoFragment());
toolbar.setTitle("视频");
break;
case 2:
fragmentTransaction.replace(R.id.ll_content, new WeatherFragment());
toolbar.setTitle("天气");
break;
}
fragmentTransaction.commit();
}
源码地址: https://github.com/miyuexingchen/Swen
更多相关文章
- Android(安卓)Inflate方法
- Android(安卓)自定义RecyclerView 实现真正的Gallery效果
- Android(安卓)listview 实现列表多选
- Android_Layout_xml布局
- Android-基本控件(五大布局 Layout)
- Android(安卓)ListView展示不同的布局
- Android(安卓)模仿renren的左右划动菜单栏
- RemoteViews用法一:widget简单用法
- [置顶] Android(安卓)SimpleAdapter,SimpleCursorAdapter,ArrayAda