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!