2015.1.7,第四节课,Android控件SlidingDrawer实现抽屉效果

//这个功能的实现 主要在布局里进行了配置

// 界面代码 利用监听setOnDrawerOpenListener 和 监听setOnDrawerCloseListener 开启与关闭

//详细代码如下:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity" >

 <!--控件设置-->
    <SlidingDrawer 
        android:id="@+id/slidingDrawer"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:content="@+id/content"        // 绑定显示的能容  通过id进行绑定
        android:handle="@+id/handle"         //绑定把手  同样通过id绑定,如果没有绑定把手会报错
        android:orientation="vertical"
         >
        <!-- 抽屉的把手部分 -->
        <LinearLayout 
            android:id="@+id/handle"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageButton 
                android:id="@+id/ib"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/open"
                android:onClick="click"
                />
        </LinearLayout>
        <!-- 抽屉的内容部分 -->
        <LinearLayout 
        android:background="@drawable/slidingdrawback"
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            >
            
            <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            >
            <ImageButton 
                android:id="@+id/upload"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/upload"
                android:onClick="click"
                />
            <ImageButton 
                android:id="@+id/insert"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/insert"
                android:onClick="click"
                />
            <ImageButton 
                android:id="@+id/help"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/help"
                android:onClick="click"
                />
            
        </LinearLayout>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:gravity="center"
            >
            <ImageButton 
                android:id="@+id/bakup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/backup"
                android:onClick="click"
                />
            <ImageButton 
                android:id="@+id/about"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/about"
                android:onClick="click"
                />
            <ImageButton 
                android:id="@+id/exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/exit"
                android:onClick="click"
                />
            
        </LinearLayout>
        </LinearLayout>
    </SlidingDrawer>


</RelativeLayout>


界面代码块

package com.example.class3_;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Toast;


public class MainActivity extends Activity {
private SlidingDrawer mSd;
private ImageButton mIb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIb=(ImageButton) findViewById(R.id.ib);
mSd=(SlidingDrawer) findViewById(R.id.slidingDrawer);

//抽屉打开时候的监听器
mSd.setOnDrawerOpenListener(new OnDrawerOpenListener() {

public void onDrawerOpened() {

                                 // 改变抽屉 样式图片
mIb.setBackgroundResource(R.drawable.close);
}
});

                //抽屉关闭时候的监听器
mSd.setOnDrawerCloseListener(new OnDrawerCloseListener() {

public void onDrawerClosed() {

                                // 改变抽屉 样式图片
mIb.setBackgroundResource(R.drawable.open);
}
});
}


public void click(View view){
switch(view.getId()){
case R.id.insert:
Toast.makeText(this, "添加", 0).show();
break;
case R.id.help:
Toast.makeText(this, "帮助", 0).show();
break;
case R.id.upload:
Toast.makeText(this, "更新", 0).show();
break;
case R.id.bakup:
Toast.makeText(this, "后退", 0).show();
break;
case R.id.about:
Toast.makeText(this, "关于", 0).show();
break;
case R.id.exit:
Toast.makeText(this, "退出", 0).show();
break;
}
}


    




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值