安卓开发SlidingDrawer实现抽屉效果

本文详细介绍了如何利用安卓系统提供的SlidingDrawer类在手机屏幕上高效展示大量信息,通过实例代码展示了如何创建抽屉效果,包括图片资源的准备、资源文件的定义、XML布局文件的编写及对应的Java文件实现,最终实现了在有限空间内展示更多内容的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在手机开发中,我们会遇到这样的问题,要在手机屏幕上显示很多信息,但是手机屏幕就那么小一点,当内容较多的时候如何显示呢,我们如何理用更有限的空间来显示更多的信息呢?我们可以使用安卓系统提供的SlidingDrawer类,使用SlidingDrawer类我们就可以借助SlidingDrawer实现抽屉效果,这就是传说中的安卓抽屉效果,下面看参考代码,以下代码经本站亲测,读者可以拷贝到Eclipse项目中运行。下面直接贴出代码。

    首先看一下运行效果:

SlidingDrawer抽屉效果实例01 SlidingDrawer抽屉效果实例02

    开始新建项目,首先准备几张图片,如下:
      1.handle_normal.png  下载
      2.handle_focused.png  下载
      3.handle_pressed.png  下载
      4.list_selector_background_pressed.png  下载

    以上图片均放置在drawable-hdpi/drawable-ldpi/drawable-mdpi/drawable-xhdpi中。

    然后我们来看代码如何写:

1.在es/drawable目录下定义一个资源文件handle.xml,参考代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=http://schemas.android.com/apk/res/android>
    <item android:state_window_focused="false"  android:drawable="@drawable/handle_normal" />
    <item android:state_focused="true" android:drawable="@drawable/handle_focused" />
    <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />
</selector>

2.在es/drawable目录下定义一个资源文件listview_selected.xml,参考代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=http://schemas.android.com/apk/res/android>
    <item android:state_pressed="true"
          android:drawable="@drawable/list_selector_background_pressed" />
</selector>

3.main.xml文件中的参考代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 <SlidingDrawer 
     android:id="@+id/slidingdrawer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:handle="@+id/handle" 
     android:content="@+id/content"> 
      <Button 
             android:id="@+id/handle" 
             android:layout_width="wrap_content" 
             android:layout_height="fill_parent" 
             android:background="@drawable/handle"  />
       <ListView
          android:id="@+id/content"
          android:layout_width="fill_parent" 
             android:layout_height="wrap_content" />
 </SlidingDrawer>
</LinearLayout>

4.在res/layout文件家中新建listview_layout.xml,文件参考代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"    > 
    <LinearLayout
     android:orientation="vertical"
     android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/listview_selected"
        android:padding="6px">
 <TextView
  android:id="@+id/webName" 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textSize="20px"
     android:textColor="#000000"/>
 <TextView
  android:id="@+id/webDescript" 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textSize="16px"
     android:textColor="#000000"/>
     </LinearLayout>
</LinearLayout>

5.对应java文件(本例是AndroidSlidingDrawerActivity.java)的参考代码:

public class AndroidSlidingDrawerActivity extends Activity {
    private ListView myListView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupViews();
    }
    private void setupViews(){
        myListView = (ListView)findViewById(R.id.content);
        myListView.setAdapter(new ListViewAdapter());
    }
    private class ListViewAdapter extends BaseAdapter{
       //这里返回10行,ListView有多少行取决于getCount()方法
       @Override
       public int getCount() {
          return 10;
       }
       @Override
       public Object getItem(int arg0) {
          return null;
       }
       @Override
       public long getItemId(int arg0) {
          return 0;
       }
       @Override
       public View getView(int position, View v, ViewGroup parent) {
          final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
          if(v == null){
              v = inflater.inflate(R.layout.listview_layout, null);
          }   
          TextView myWebName = (TextView)v.findViewById(R.id.webName);
          TextView myWebDescript = (TextView)v.findViewById(R.id.webDescript);
          myWebName.setText("Android开发学习网" + position);
          myWebDescript.setText("slidingdrawer用法详解" + position);
          return v;
       }
    }
}

运行即可看到效果.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值