- 注册时间
- 2011-3-22
- 最后登录
- 2013-6-27
- 在线时间
- 11644 小时
- 阅读权限
- 150
- 积分
- 62779
- 帖子
- 28923
- 精华
- 1
- UID
- 6
|
SlidingDrawer 有俩个重要的属性 handle、content 一个头 一个内容。效果就是抽屉样子。给个效果图
下面看代码:- <?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>
复制代码 这里就可以看到抽屉效果了。
下面我们结束背景图片怎么控制。
当点击抽屉头的时候背景色为黄色、默认为灰色。listview选中当中一项颜色为黄色。默认白色。
下面给出 图片素材。
把图片复制到 drawable-hdpi 目录下。
在res下面新建文件夹drawable,然后在这个夹子下面分别建handle.xml listview_selected.xml
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>
复制代码 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" /> <!--listview点击选择一项时候-->
- </selector>
-
复制代码 准备好素材和样式。下面看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">
- <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" /> <!--就是上面建的handle.xml-->
- <ListView android:id="@+id/content" android:layout_width="fill_parent"
- android:layout_height="wrap_content" /> <!--这里定义个listview-->
- </SlidingDrawer>
- </LinearLayout>
复制代码 另外新建一个layout xml文件 用于填充listview- <?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/bookname"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="20px"
- android:textColor="#000000"
- />
- <TextView
- android:id="@+id/author"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="16px"
- android:textColor="#000000"
- />
- </LinearLayout>
- </LinearLayout>
复制代码 下面看java代码:- package com.qwj.MyAndroid.bll;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ListViewStyle extends Activity {
- private ListView mListView;
- private TextView txtMessage;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.listview_mian);
- setupViews();
- }
- private void setupViews() {
- mListView = (ListView) findViewById(R.id.content);
- mListView.setAdapter(new ListViewAdapter());
- mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(android.widget.AdapterView<?> arg0,
- View arg1, int arg2, long arg3) {
- try {
- txtMessage = (TextView) arg1.findViewById(R.id.author);
- Toast.makeText(ListViewStyle.this,
- txtMessage.getText().toString(), Toast.LENGTH_LONG)
- .show();
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- };
- });
- }
- private class ListViewAdapter extends BaseAdapter {
- // 这里返回10行,ListView有多少行取决于getCount()方法
- public int getCount() {
- return 10;
- }
- public Object getItem(int arg0) {
- return null;
- }
- public long getItemId(int arg0) {
- return 0;
- }
- 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 mBookName = (TextView) v.findViewById(R.id.bookname);
- TextView mBookAuthor = (TextView) v.findViewById(R.id.author);
- mBookName.setText(position + "Android教程 谁是傻瓜: ");
- mBookAuthor.setText("当然是漠漠哦!" + position);
- return v;
- }
- }
- }
复制代码 前面已经讲过BaseAdapter 用法了。
这里加了Toast.makeText(ListViewStyle.this,txtMessage.getText().toString(), Toast.LENGTH_LONG).show(); 显示一个层一样的漂浮消息。
效果图:
|
-
2
查看全部评分
-
|