安卓開發(fā)--左側(cè)或右側(cè)出來的popuwindows替代drawerlayout側(cè)拉菜單

開發(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 ~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市福侈,隨后出現(xiàn)的幾起案子酒来,更是在濱河造成了極大的恐慌,老刑警劉巖肪凛,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件堰汉,死亡現(xiàn)場離奇詭異,居然都是意外死亡伟墙,警方通過查閱死者的電腦和手機(jī)衡奥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來远荠,“玉大人矮固,你說我怎么就攤上這事∑┐荆” “怎么了档址?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵,是天一觀的道長邻梆。 經(jīng)常有香客問我守伸,道長,這世上最難降的妖魔是什么浦妄? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任尼摹,我火速辦了婚禮,結(jié)果婚禮上剂娄,老公的妹妹穿的比我還像新娘蠢涝。我一直安慰自己,他們只是感情好阅懦,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布和二。 她就那樣靜靜地躺著,像睡著了一般耳胎。 火紅的嫁衣襯著肌膚如雪惯吕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天怕午,我揣著相機(jī)與錄音废登,去河邊找鬼。 笑死郁惜,一個(gè)胖子當(dāng)著我的面吹牛堡距,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼吏颖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了恨樟?” 一聲冷哼從身側(cè)響起半醉,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎劝术,沒想到半個(gè)月后缩多,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡养晋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年衬吆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绳泉。...
    茶點(diǎn)故事閱讀 40,973評論 1 354
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡逊抡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出零酪,到底是詐尸還是另有隱情冒嫡,我是刑警寧澤,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布四苇,位于F島的核電站孝凌,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏月腋。R本人自食惡果不足惜蟀架,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望榆骚。 院中可真熱鬧片拍,春花似錦、人聲如沸妓肢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽职恳。三九已至所禀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間放钦,已是汗流浹背色徘。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留操禀,地道東北人褂策。 一個(gè)月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親斤寂。 傳聞我的和親對象是個(gè)殘疾皇子耿焊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容