自定义SlidingDrawer抽屉式效果和SlidingDrawer抽屉式效果

来源:互联网 发布:淘宝客户信息购买 编辑:程序博客网 时间:2024/04/29 12:37

 

1.自定义的SlidingDrawer抽屉式效果

 

效果1

 

 

效果2

效果3

 

 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.cn.npass.nj"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".SlidingDrawerDemoActivity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 

wrapsliding.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@color/gainsboro"    android:orientation="vertical" >    <com.cn.npass.nj.WrapSlidingDrawer        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:layout_alignParentRight="true"        android:content="@+id/content"        android:handle="@+id/handle"        android:orientation="vertical" >        <Button            android:id="@+id/handle"            android:layout_width="30dip"            android:layout_height="30dip"            android:background="@drawable/help" />        <LinearLayout            android:id="@+id/content"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:background="@color/peachpuff"            android:orientation="horizontal" >            <Button                android:id="@+id/button1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="按钮1" /><Button                android:id="@+id/button2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="按钮2" />                    </LinearLayout>    </com.cn.npass.nj.WrapSlidingDrawer></RelativeLayout>


 

 

SlidingDrawerDemoActivity.java

package com.cn.npass.nj;import android.app.Activity;import android.os.Bundle;public class SlidingDrawerDemoActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);        setContentView(R.layout.wrapsliding);    }}


 

WrapSlidingDrawer.java

package com.cn.npass.nj;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.SlidingDrawer;public class WrapSlidingDrawer extends SlidingDrawer {      private boolean mVertical;      private int mTopOffset;            public WrapSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {          super(context, attrs, defStyle);          int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);          mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);          mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);      }        public WrapSlidingDrawer(Context context, AttributeSet attrs) {          super(context, attrs);          int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);          mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);         mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);      }        @Override      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {          int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);          int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);          int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);          int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);            final View handle = getHandle();          final View content = getContent();         measureChild(handle, widthMeasureSpec, heightMeasureSpec);            if (mVertical) {              int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;              content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode));             heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();              widthSpecSize = content.getMeasuredWidth();              if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();          }          else {              int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;              getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec);             widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();              heightSpecSize = content.getMeasuredHeight();              if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();          }            setMeasuredDimension(widthSpecSize, heightSpecSize);     }  }  

 

资源下载地址:http://download.csdn.net/detail/niejing654092427/4498795

 

 

2.SlidingDrawer抽屉式效果

效果图1

 

 

效果图2

 

 

 

 

 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.cn.npass"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".MainActivity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:background="@drawable/bg">    <SlidingDrawer        android:id="@+id/slidingdrawer"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:handle="@+id/handle"        android:content="@+id/content"        android:orientation="vertical" >        <!--         说明:        android:handle="@+id/handle" 是可以拖动的handle         android:content="@+id/content"  是可以隐藏内容的View        -->        <Button            android:id="@+id/handle"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/icon2" />        <ListView            android:id="@+id/content"            android:layout_width="fill_parent"            android:layout_height="fill_parent" />    </SlidingDrawer></LinearLayout>


listview_item.xml

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


MainActivity.java

package com.cn.npass;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {private ListView mListView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);setupViews();}private void setupViews() {mListView = (ListView) findViewById(R.id.content);mListView.setAdapter(new ListViewAdapter());}private class ListViewAdapter extends BaseAdapter {// 这里返回50行,ListView有多少行取决于getCount()方法public int getCount() {return 50;}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_item, null);}TextView mBookName = (TextView) v.findViewById(R.id.bookname);TextView mBookAuthor = (TextView) v.findViewById(R.id.author);mBookName.setText("Android教程" + position);mBookAuthor.setText("ss" + position);return v;}}}

 

资源下载地址为: http://download.csdn.net/detail/niejing654092427/4504039

 


 

原创粉丝点击