開發(fā)中,由于項(xiàng)目需求啊送,需要每一個(gè)fragment都有側(cè)拉欄死遭,第一時(shí)間想到的就是用drawerlayout广恢,這是谷歌推薦的側(cè)拉欄,用法非常簡單呀潭,只要把drawerlayout作為布局的最外層钉迷,然后里面用兩個(gè)大布局包著,一個(gè)是主布局钠署,一個(gè)是側(cè)拉菜單布局糠聪,然后再側(cè)拉布局的父布局給個(gè)屬性 :layout_gravity="left",這樣用手勢側(cè)拉就能拉出來側(cè)拉欄了谐鼎,可以和toobar綁定枷颊,通過toggle
//獲取開關(guān)同時(shí)讓開關(guān)和DrawerLayout關(guān)聯(lián)在一起toggle = new
ActionBarDrawerToggle(this, mDrawerLayout, 0, 0);
getSupportActionBar()
.setDisplayHomeAsUpEnabled(true);//設(shè)置默認(rèn)的標(biāo)題不顯示
getSupportActionBar()
.setDisplayShowTitleEnabled(false);//設(shè)置點(diǎn)擊事件,點(diǎn)擊彈出menu界面
mDrawerLayout.setDrawerListener(toggle);
不過正常四個(gè)fragment的話该面,底部導(dǎo)航按鈕是不會(huì)被遮擋住的夭苗,這樣拉出菜單底下按鈕還可以點(diǎn)擊切換,需求不允許隔缀,所以就考慮從簡题造,用popuwindows代替?zhèn)壤瓩?br> 先看效果圖:
其實(shí)大體邏輯上并不難,只是自定義popuwindos猾瘸,然后動(dòng)畫樣式設(shè)置是左到右界赔,或者右到左,這種滑入滑出的感覺牵触,然后監(jiān)聽dismiss淮悼,控制窗口透明度,就是周圍變暗揽思,突出菜單欄袜腥,點(diǎn)擊事件可以構(gòu)造方法傳入包括更新popuwinds上的頭像姓名也可以設(shè)置get方法拿到對象,
public TextView getStartview(){ return starttime;}
然后調(diào)用update()更新
popMenus2.getStartview().setText(pickstarttimet);
popMenus2.update();
popMenus2.showAtLocation(MainActivity.this.findViewById(R.id.*main_layout*),
Gravity.*BOTTOM *| Gravity.*CENTER_HORIZONTAL*, 0, 0);
接下來上代碼:
1.布局
主布局就直接 relativlayout 替代標(biāo)題(style是NoActionbar)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/titbar_color">
<ImageView
android:id="@+id/lefthaha"
android:clickable="true"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/title_but_left3x"/>
<TextView
android:id="@+id/titlehaha"
android:text="我是主題"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/righthaha"
android:clickable="true"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/title_but_right3x"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
leftpopuwindows布局
不貼了就是普通從上到下的linerlayout
代碼:
public class LeftPopupWindows extends PopupWindow {
private View mMenuView; // PopupWindow 菜單布局
private Context context; // 上下文參數(shù)
private OnClickListener myOnClick; // PopupWindow 菜單 空間單擊事件
private LinearLayout shenqing;
private LinearLayout shenpi;
private LinearLayout gongxiangwj;
private LinearLayout exit;
private CircleImageView touxiang;
private TextView name;
private ImageView shenqing_red,shenpi_red,gongxiangwj_red;
public LeftPopupWindows(Activity context, OnClickListener myOnClick) {
super(context);
this.context = context;
this.myOnClick = myOnClick;
Init();
}
private void Init() {
// TODO Auto-generated method stub
// PopupWindow 導(dǎo)入
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.leftpopuwindows, null);
shenqing = (LinearLayout) mMenuView.findViewById(R.id.shenqing);
shenpi = (LinearLayout) mMenuView.findViewById(R.id.shenpi);
gongxiangwj = (LinearLayout) mMenuView.findViewById(R.id.gongxiangwj);
name = (TextView) mMenuView.findViewById(R.id.leftname);
touxiang = (CircleImageView) mMenuView.findViewById(R.id.leftphoto);
shenpi_red = (ImageView) mMenuView.findViewById(R.id.shenpi_red);
shenqing_red = (ImageView) mMenuView.findViewById(R.id.shenqing_red);
gongxiangwj_red = (ImageView) mMenuView.findViewById(R.id.gongxiangwj_red);
exit = (LinearLayout) mMenuView.findViewById(R.id.exit);
touxiang.setOnClickListener(myOnClick);
//審批
shenpi.setOnClickListener(myOnClick);
//申請
shenqing.setOnClickListener(myOnClick);
//共享文件
gongxiangwj.setOnClickListener(myOnClick);
//退出
exit.setOnClickListener(myOnClick);
// 導(dǎo)入布局
this.setContentView(mMenuView);
// 設(shè)置動(dòng)畫效果
this.setAnimationStyle(R.style.AnimationLeftFade);
//防止虛擬鍵擋住
this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//設(shè)置彈出窗體的 寬钉汗,高
this.setWidth(LayoutParams.WRAP_CONTENT);
this.setHeight(LayoutParams.MATCH_PARENT);
// 設(shè)置可觸
this.setFocusable(true);
ColorDrawable dw = new ColorDrawable(0x0000000);
this.setBackgroundDrawable(dw);
// 單擊彈出窗以外處 關(guān)閉彈出窗
mMenuView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
setWindowAlpa(false);
}
}
return true;
}
});
}
/**
* 動(dòng)態(tài)設(shè)置Activity背景透明度
*
* @param isopen
*/
public void setWindowAlpa(boolean isopen) {
if (Build.VERSION.SDK_INT < 11) {
return;
}
final Window window = ((Activity) context).getWindow();
final WindowManager.LayoutParams lp = window.getAttributes();
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
ValueAnimator animator;
if (isopen) {
animator = ValueAnimator.ofFloat(1.0f, 0.5f);
} else {
animator = ValueAnimator.ofFloat(0.5f, 1.0f);
}
animator.setDuration(400);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float alpha = (float) animation.getAnimatedValue();
lp.alpha = alpha;
window.setAttributes(lp);
}
});
animator.start();
}
使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lefticon = (ImageView) findViewById(R.id.lefthaha);
righticon = (ImageView) findViewById(R.id.righthaha);
title = (TextView) findViewById(R.id.titlehaha);
mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
title.setText("你是電羹令,你是光鲤屡,你是唯一的智障");
lefticon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
leftPopupWindows = new LeftPopupWindows(MainActivity.this,leftonclick );
leftPopupWindows.showAtLocation(mainlayout, Gravity.LEFT,0,0);
leftPopupWindows.setWindowAlpa(true);
leftPopupWindows.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
leftPopupWindows.setWindowAlpa(false);
}
});
}
});
righticon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rightpopuwindows = new RightPopupWindows(MainActivity.this,rightonclick );
rightpopuwindows.showAtLocation(mainlayout, Gravity.RIGHT,0,0);
rightpopuwindows.setWindowAlpa(true);
rightpopuwindows.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
rightpopuwindows.setWindowAlpa(false);
}
});
}
});
}
private View.OnClickListener leftonclick = new View.OnClickListener() {
@Override
public void onClick(View v) {
leftPopupWindows.dismiss();
switch (v.getId()) {
case R.id.leftphoto:
Toast.makeText(MainActivity.this, "頭像", Toast.LENGTH_SHORT).show();
break;
case R.id.shenqing:
Toast.makeText(MainActivity.this, "菜單1", Toast.LENGTH_SHORT).show();
break;
case R.id.shenpi:
Toast.makeText(MainActivity.this, "菜單2", Toast.LENGTH_SHORT).show();
break;
case R.id.gongxiangwj:
Toast.makeText(MainActivity.this, "菜單3", Toast.LENGTH_SHORT).show();
break;
case R.id.exit:
Toast.makeText(MainActivity.this, "退出", Toast.LENGTH_SHORT).show();
break;
}
}
};
private View.OnClickListener rightonclick = new View.OnClickListener() {
@Override
public void onClick(View v) {
rightpopuwindows.dismiss();
switch (v.getId()) {
case R.id.leftphoto:
Toast.makeText(MainActivity.this, "頭像", Toast.LENGTH_SHORT).show();
break;
case R.id.shenqing:
Toast.makeText(MainActivity.this, "菜單1", Toast.LENGTH_SHORT).show();
break;
case R.id.shenpi:
Toast.makeText(MainActivity.this, "菜單2", Toast.LENGTH_SHORT).show();
break;
case R.id.gongxiangwj:
Toast.makeText(MainActivity.this, "菜單3", Toast.LENGTH_SHORT).show();
break;
case R.id.exit:
Toast.makeText(MainActivity.this, "退出", Toast.LENGTH_SHORT).show();
break;
}
}
};
動(dòng)畫樣式
<!--左側(cè)出來的popuwindow-->
<style name="AnimationLeftFade">
<item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
<item name="android:windowExitAnimation">@anim/out_righttoleft</item>
</style>
<!--右側(cè)出來的popuwindow-->
<style name="AnimationRightFade">
<item name="android:windowEnterAnimation">@anim/in_righttoleft</item>
<item name="android:windowExitAnimation">@anim/out_lefttoright</item>
</style>
<!--底下出來的popuwindow-->
<style name="AnimationBottomFade">
<item name="android:windowEnterAnimation">@anim/in_bottomtotop</item>
<item name="android:windowExitAnimation">@anim/out_toptobottom</item>
</style>
demo下載地址 :
https://github.com/PangHaHa12138/LeftPopuwindowsDemo
感謝閱讀 ~have a nice day ~