Android slidingdrawer achieves drawer Effect

Source: Internet
Author: User

Usually we need to display more information on the user's screen, but the user's screen size is limited. How can we use limited space to display more information? Android provides us with the SlidingDrawer class to help us easily achieve the desired effect. Such as moji recommendations in moji weather, 360 security guards are all reflected.

Next we will learn about the SlidingDrawer class to achieve the above effect:

:

When not expanded:


After expansion:


See the code below:

First, layout files:

<?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="@drawable/splash_background"    ><SlidingDrawer android:id="@+id/slidingDrawer"android:layout_width="fill_parent"android:layout_height="wrap_content"android:handle="@+id/handle"android:content="@+id/content"><LinearLayout android:id="@+id/content"android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/empty"></LinearLayout><Button android:id="@+id/handle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" open "android:textSize="24dip"/></SlidingDrawer></LinearLayout>

Main attributes:
Android: handle = "@ + id/handle" is equivalent to expanding or detaching a Button. Here I use a Button to demonstrate (TextView and other functions are acceptable ), it is better to use ImageView in a project. You only need to set the icon style when you expand or close it.

Android: content = "@ + id/content.


Main Code:

Package com. cloay. slidingdrawer; import android. app. activity; import android. OS. bundle; import android. widget. button; import android. widget. slidingDrawer; import android. widget. slidingDrawer. onDrawerCloseListener; import android. widget. slidingDrawer. onDrawerOpenListener;/*** drawer effect demo * sjavasingdrawertestactivity. java * @ author cloay * 2012-1-30 */public class SlidingDrawerTestActivity extends Activity {/** Called when the activity is first created. */private Button button; private SlidingDrawer slidingDrawer; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); button = (Button) findViewById (R. id. handle); slidingDrawer = (SlidingDrawer) findViewById (R. id. slidingDrawer); slidingDrawer. setOnDrawerCloseListener (new OnDrawerCloseListener () {// reclaim and perform some processing @ Overridepublic void onDrawerClosed () {button. setText ("Open"); // here I will change the text prompt to Open}); slidingDrawer. setOnDrawerOpenListener (new OnDrawerOpenListener () {// do some processing when expanding @ Overridepublic void onDrawerOpened () {button. setText ("Close"); // here I change the text prompt to Close }});}}

It's easy. You don't need to write more! If you have any questions, please leave a message for us to learn and exchange!

Note: Please indicate the source for reprinting!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.