左侧滑动窗口是很多app应用经常用到的,学习它也是很有必要的。
左侧滑动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" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="true" android:fitsSystemWindows="true" > <include android:id="@+id/rl_title" layout="@layout/layout_main_title" /> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/rl_title" android:background="@color/white_normal" > </FrameLayout> </RelativeLayout> <!-- The navigation view --> <FrameLayout android:id="@+id/left_drawer" android:layout_width="280dp" android:layout_height="match_parent" android:layout_gravity="start" > <include layout="@layout/layout_main_left" /> </FrameLayout> <!-- <ListView --> <!-- android:id="@+id/left_drawer" --> <!-- android:layout_width="240dp" --> <!-- android:layout_height="match_parent" --> <!-- android:layout_gravity="start" --> <!-- android:background="#ffffcc" --> <!-- android:choiceMode="singleChoice" --> <!-- android:divider="@android:color/transparent" --> <!-- android:dividerHeight="0dp" > --> <!-- </ListView> --> </android.support.v4.widget.DrawerLayout>
include是连接一个新的页面的布局,对于复杂的页面设计,可以起到很好地效果,这样使得结构更清晰,更具有可读性。
FrameLayout是一个框架,也可以把它看着一个activity。上面是一个整体的框架布局,一般把主体部分放在左滑动菜单前面,
这样更利于编辑是区分。
下面是上面xml的效果图:
绿框就是默认的菜单位置。
Main的代码:
package com.ansen.developerheadlines.activity; import android.os.Build; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.RelativeLayout; import com.ansen.developerheadlines.R; import com.ansen.developerheadlines.fragment.ContentFragment; import com.ansen.developerheadlines.fragment.GiftFragment; import com.ansen.developerheadlines.fragment.MainFragment; import com.ansen.developerheadlines.fragment.ShareFragment; public class MainActivity extends FragmentActivity{ private DrawerLayout mDrawerLayout; private RelativeLayout rlHome, rlGift, rlShare; private int currentSelectItem = R.id.rl_home;// 默认首页 private MainFragment mainFragment; private ShareFragment shareFragment; private GiftFragment giftFragment; // private ContentFragment contentFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); findViewById(R.id.iv_menu).setOnClickListener(clickListener); initLeftMenu();//初始化左边菜单 // contentFragment=new ContentFragment(); mainFragment=new MainFragment(); // getSupportFragmentManager().beginTransaction().add(R.id.content_frame,contentFragment).commit(); getSupportFragmentManager().beginTransaction().add(R.id.content_frame,mainFragment).commit(); setWindowStatus(); } private void initLeftMenu() { rlHome = (RelativeLayout) findViewById(R.id.rl_home); rlGift = (RelativeLayout) findViewById(R.id.rl_gift); rlShare = (RelativeLayout) findViewById(R.id.rl_share); rlHome.setOnClickListener(onLeftMenuClickListener); rlGift.setOnClickListener(onLeftMenuClickListener); rlShare.setOnClickListener(onLeftMenuClickListener); rlHome.setSelected(true); } private OnClickListener onLeftMenuClickListener = new OnClickListener() { @Override public void onClick(View v) { if (currentSelectItem != v.getId()) {//防止重复点击 currentSelectItem=v.getId(); noItemSelect(); changeFragment(v.getId());//设置fragment显示切换 switch (v.getId()) { case R.id.rl_home: rlHome.setSelected(true); // contentFragment.setContent("这是首页"); break; case R.id.rl_gift: rlGift.setSelected(true); // contentFragment.setContent("这是礼物"); break; case R.id.rl_share: rlShare.setSelected(true); // contentFragment.setContent("这是分享"); break; } mDrawerLayout.closeDrawer(Gravity.LEFT); } } }; /** * 改变fragment的显示 * @param resId // */ private void changeFragment(int resId) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个Fragment事务 hideFragments(transaction);// t if(resId==R.id.rl_home){//主页 if(mainFragment==null){//如果为空先添加进来.不为空直接显示 mainFragment = new MainFragment(); transaction.add(R.id.content_frame,mainFragment); }else { transaction.show(mainFragment); } }else if(resId==R.id.rl_share){ if(shareFragment==null){ shareFragment = new ShareFragment(); transaction.add(R.id.content_frame,shareFragment);//加入后直接调用了show()显示。 }else { transaction.show(shareFragment); } }else if(resId==R.id.rl_gift){ if(giftFragment==null){ giftFragment = new GiftFragment(); transaction.add(R.id.content_frame,giftFragment); }else { transaction.show(giftFragment); } } transaction.commitAllowingStateLoss();//一定要记得提交事务 } /** * 显示之前隐藏所有fragment * @param transaction */ private void hideFragments(FragmentTransaction transaction){ if (mainFragment != null)//不为空才隐藏,如果不判断第一次会有空指针异常 transaction.hide(mainFragment); if (shareFragment != null) transaction.hide(shareFragment); if (giftFragment != null) transaction.hide(giftFragment); } private void noItemSelect(){ rlHome.setSelected(false); rlGift.setSelected(false); rlShare.setSelected(false); } private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_menu:// 打开左边抽屉 mDrawerLayout.openDrawer(Gravity.LEFT); break; } } }; // 设置状态栏 private void setWindowStatus() { // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // // 透明状态栏 // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // // 透明导航栏 // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); // // 设置状态栏颜色 // getWindow().setBackgroundDrawableResource(R.color.main_color); // } } }
这些只是部分代码,需要进一步补充才能看到效果。