设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 5176|回复: 10
打印 上一主题 下一主题

android 开发 SlidingDrawer 使用 [复制链接]

Rank: 8Rank: 8

跳转到指定楼层
1#
那个谁 发表于 2011-5-25 14:05:31 |只看该作者 |倒序浏览
SlidingDrawer 有俩个重要的属性 handle、content 一个头 一个内容。效果就是抽屉样子。给个效果图
3.png
下面看代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <SlidingDrawer android:id="@+id/slidingdrawer"
  6.                 android:layout_width="fill_parent" android:layout_height="fill_parent"
  7.                 android:orientation="horizontal" android:handle="@+id/handle"
  8.                 android:content="@+id/content">
  9.                  <!--抽屉头-->
  10.                 <Button android:id="@+id/handle" android:layout_width="wrap_content"
  11.                         android:layout_height="fill_parent" android:background="@drawable/handle" />  
  12.                   <!--抽屉内容-->
  13.                 <ListView android:id="@+id/content" android:layout_width="fill_parent"
  14.                         android:layout_height="wrap_content" />
  15.         </SlidingDrawer>
  16. </LinearLayout>  
复制代码
这里就可以看到抽屉效果了。
下面我们结束背景图片怎么控制。
当点击抽屉头的时候背景色为黄色、默认为灰色。listview选中当中一项颜色为黄色。默认白色。
下面给出 图片素材。
handle_pressed.gif handle_normal.gif handle_focused.gif list_selector_background_pressed.gif
把图片复制到 drawable-hdpi 目录下。
在res下面新建文件夹drawable,然后在这个夹子下面分别建handle.xml listview_selected.xml 
handle.xml代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_window_focused="false"  android:drawable="@drawable/handle_normal" />   <!--抽屉关着时候-->
  4.     <item android:state_focused="true" android:drawable="@drawable/handle_focused" />    <!--抽屉点击时候-->
  5.     <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />   <!--抽屉关闭时候-->
  6. </selector>
复制代码
listview_selected.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.         <item android:state_pressed="true"
  4.                 android:drawable="@drawable/list_selector_background_pressed" />   <!--listview点击选择一项时候-->
  5. </selector>  
复制代码
准备好素材和样式。下面看layout xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <SlidingDrawer android:id="@+id/slidingdrawer"
  6.                 android:layout_width="fill_parent" android:layout_height="fill_parent"
  7.                 android:orientation="horizontal" android:handle="@+id/handle"
  8.                 android:content="@+id/content">
  9.                 <Button android:id="@+id/handle" android:layout_width="wrap_content"
  10.                         android:layout_height="fill_parent" android:background="@drawable/handle" /> <!--就是上面建的handle.xml-->
  11.                 <ListView android:id="@+id/content" android:layout_width="fill_parent"
  12.                         android:layout_height="wrap_content" />   <!--这里定义个listview-->
  13.         </SlidingDrawer>
  14. </LinearLayout>  
复制代码
另外新建一个layout xml文件 用于填充listview
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:orientation="vertical"  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="fill_parent"  
  6.         android:background="#ffffff"      
  7.         >   
  8.         <LinearLayout  
  9.             android:orientation="vertical"  
  10.             android:layout_width="fill_parent"  
  11.             android:layout_height="fill_parent"  
  12.             android:background="@drawable/listview_selected"  
  13.             android:padding="6px"  
  14.         >  
  15.         <TextView  
  16.             android:id="@+id/bookname"   
  17.             android:layout_width="fill_parent"   
  18.             android:layout_height="wrap_content"   
  19.             android:textSize="20px"  
  20.             android:textColor="#000000"  
  21.             />  
  22.         <TextView  
  23.             android:id="@+id/author"   
  24.             android:layout_width="fill_parent"   
  25.             android:layout_height="wrap_content"   
  26.             android:textSize="16px"  
  27.             android:textColor="#000000"  
  28.             />  
  29.             </LinearLayout>  
  30.     </LinearLayout>  
复制代码
下面看java代码:
  1. package com.qwj.MyAndroid.bll;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.AdapterView;
  8. import android.widget.AdapterView.OnItemClickListener;
  9. import android.widget.BaseAdapter;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;

  13. public class ListViewStyle extends Activity {
  14.         private ListView mListView;
  15.         private TextView txtMessage;

  16.         @Override
  17.         public void onCreate(Bundle savedInstanceState) {
  18.                 super.onCreate(savedInstanceState);
  19.                 setContentView(R.layout.listview_mian);

  20.                 setupViews();
  21.         }

  22.         private void setupViews() {
  23.                 mListView = (ListView) findViewById(R.id.content);
  24.                 mListView.setAdapter(new ListViewAdapter());
  25.                 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  26.                         @Override
  27.                         public void onItemClick(android.widget.AdapterView<?> arg0,
  28.                                         View arg1, int arg2, long arg3) {
  29.                                 try {
  30.                                         txtMessage = (TextView) arg1.findViewById(R.id.author);
  31.                                         Toast.makeText(ListViewStyle.this,
  32.                                                         txtMessage.getText().toString(), Toast.LENGTH_LONG)
  33.                                                         .show();

  34.                                 } catch (Exception ex) {
  35.                                         System.out.println(ex.getMessage());
  36.                                 }
  37.                         };
  38.                 });
  39.         }

  40.         private class ListViewAdapter extends BaseAdapter {
  41.                 // 这里返回10行,ListView有多少行取决于getCount()方法
  42.                 public int getCount() {
  43.                         return 10;
  44.                 }

  45.                 public Object getItem(int arg0) {
  46.                         return null;
  47.                 }

  48.                 public long getItemId(int arg0) {
  49.                         return 0;
  50.                 }

  51.                 public View getView(int position, View v, ViewGroup parent) {

  52.                         final LayoutInflater inflater = LayoutInflater
  53.                                         .from(getApplicationContext());

  54.                         if (v == null) {
  55.                                 v = inflater.inflate(R.layout.listview_layout, null);
  56.                         }
  57.                         TextView mBookName = (TextView) v.findViewById(R.id.bookname);
  58.                         TextView mBookAuthor = (TextView) v.findViewById(R.id.author);

  59.                         mBookName.setText(position + "Android教程 谁是傻瓜: ");
  60.                         mBookAuthor.setText("当然是漠漠哦!" + position);
  61.                         return v;
  62.                 }

  63.         }
  64. }
复制代码
前面已经讲过BaseAdapter  用法了。
这里加了Toast.makeText(ListViewStyle.this,txtMessage.getText().toString(), Toast.LENGTH_LONG).show(); 显示一个层一样的漂浮消息。
效果图:
2.png 1.png `1.png
2

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

Rank: 8Rank: 8

2#
momo 发表于 2011-5-25 14:09:02 |只看该作者
混混不能再给杰杰分了
他要超过我的分了
过了爱做梦的年纪
轰轰烈烈不如平静

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

3#
混混@普宁.中国 实名认证  发表于 2011-5-25 14:16:49 |只看该作者


那好,这帖不给加分了

使用道具 举报

Rank: 8Rank: 8

4#
momo 发表于 2011-5-25 14:40:41 |只看该作者
{:3_59:}欧磊磊,哦啦啦
不给分不给分,
过了爱做梦的年纪
轰轰烈烈不如平静

使用道具 举报

Rank: 8Rank: 8

5#
那个谁 发表于 2011-5-25 15:57:27 |只看该作者
混混@普宁.中国 发表于 2011-5-25 14:16
那好,这帖不给加分了

不给分。对得这么字不。。。让别人看了情何以堪啊。。。。。

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

6#
混混@普宁.中国 实名认证  发表于 2011-5-25 16:08:48 |只看该作者
那个谁 发表于 2011-5-25 15:57
不给分。对得这么字不。。。让别人看了情何以堪啊。。。。。

冤有头 债有主



那个杰,请锁定摸摸。。。

使用道具 举报

Rank: 7Rank: 7Rank: 7

7#
俊俊 实名认证  发表于 2011-5-26 15:14:30 |只看该作者
原来有人在搞ANDROID开发!找到一点来这个论坛的意义了!

使用道具 举报

Rank: 8Rank: 8

8#
那个谁 发表于 2011-5-26 16:52:31 |只看该作者
cjqq0218 发表于 2011-5-26 15:14
原来有人在搞ANDROID开发!找到一点来这个论坛的意义了!

欢迎指教。。。这个论坛还有很多你想不到的哦。。。。

使用道具 举报

Rank: 7Rank: 7Rank: 7

9#
俊俊 实名认证  发表于 2011-5-26 17:40:27 |只看该作者
那个谁 发表于 2011-5-26 16:52
欢迎指教。。。这个论坛还有很多你想不到的哦。。。。

我是来学习来了!

使用道具 举报

Rank: 7Rank: 7Rank: 7

10#
俊俊 实名认证  发表于 2011-7-27 14:16:29 |只看该作者
cjqq0218 发表于 2011-5-26 17:40
我是来学习来了!

结果我是来带领你们玩来了~~罪过啊,,,罪过啊!!!

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-4-25 08:16 , Processed in 1.109500 second(s), 17 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部