Android UI: How to Use the hidden layout drawer in Android

Source: Internet
Author: User

Recently I am writing an application and want to put the setting page and the application page together so that users can see the impact of their settings on the UI in real time, in this way, you can easily set your favorite interface. After thinking for a while, I found that using the slidingdrawer control can achieve this effect. That is, a drawer. Open the drawer and occupy half of the screen. The other half of the screen is displayed on the application page. The results are good.

Today I will share with you the drawer effect in Android. In fact, lanucher in Android is a drawer. You can open it to see the installed application. I believe everyone has ever seen it. Next we will make the same effect, of course, just the same effect on the UI.

The slidingdrawer control is easy to use and can be basically configured in XML. The Code is as follows.

<? 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"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: textsize = "20sp"
/>
<Slidingdrawer
Android: Id = "@ + ID/sd"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: handle = "@ + ID/IV"
Android: content = "@ + ID/mycontent"
Android: Orientation = "vertical"
>

<Imageview
Android: Id = "@ + ID/IV"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/open1"
/>

<Gridview
Android: Id = "@ ID/mycontent"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: numcolumns = "3"
Android: Background = "@ drawable/background"
Android: gravity = "center"
/>

</Slidingdrawer>
</Relativelayout>

 

Android: handle: indicates the image of the drawer under the slidingdrawer label. Android: Content: The layout in the drawer. With this layout, a drawer came out.

Let's take a look at the chouti class code.

Public class chouti extends activity {
 
Private gridview GV;
Private slidingdrawer SD;
Private imageview IV;
Private int [] icons = {R. drawable. browser, R. drawable. gallery,
R. drawable. Camera, R. drawable. Gmail,
R. drawable. Music, R. drawable. Market,
R. drawable. Phone, R. drawable. Messages, R. drawable. Maps };
Private string [] items = {"Browser", "image", "camera", "Clock", "Music", "market", "dialing", "information ", "map "};

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
GV = (gridview) findviewbyid (R. Id. mycontent );
SD = (s0000ingdrawer) findviewbyid (R. Id. SD );
IV = (imageview) findviewbyid (R. Id. IV );
Myadapter adapter = new myadapter (this, items, icons); // customize myadapter to display the icon and item
GV. setadapter (adapter );
SD. setondraweropenlistener (New s0000ingdrawer. ondraweropenlistener () // open the drawer
{
@ Override
Public void ondraweropened ()
{
Iv. setimageresource (R. drawable. close1); // in response to the drawer opening event, set the image to
}
});
SD. setondrawercloselistener (New slidingdrawer. ondrawercloselistener ()
{
@ Override
Public void ondrawerclosed ()
{
Iv. setimageresource (R. drawable. open1); // responds to the close drawer event
}
});
}
}

Import the layout in the entire class, and set monitoring events for the switch drawer. Here we need to customize a myadapter to display the image with the text subject.

The following is the code of the myadapter class.

Public class myadapter extends baseadapter
{
Private context _ CT;
Private string [] _ items;
Private int [] _ icons;

Public myadapter (context CT, string [] items, int [] icons) // Constructor
{
_ Ct = CT;
_ Items = items;
_ Icons = icons;
}

@ Override
Public int getcount ()
{
Return _ items. length;
}

@ Override
Public object getitem (INT arg0)
{
Return _ items [arg0];
}

@ Override
Public long getitemid (INT position)
{
Return position;
}

@ Override
Public View getview (INT position, view convertview, viewgroup parent)
{
Layoutinflater factory = layoutinflater. From (_ CT );
View v = (View) Factory. Inflate (R. layout. gv, null); // bind custom Layout
Imageview IV = (imageview) v. findviewbyid (R. Id. Icon );
Textview TV = (textview) v. findviewbyid (R. Id. Text );
Iv. setimageresource (_ icons [position]);
TV. settext (_ items [position]);
Return V;
}
}

It is also very simple, and the layout used is as follows:

<? 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"
>
<Imageview
Android: Id = "@ + ID/icon"
Android: layout_width = "wrap_content"
Android: layout_height = "40px"
Android: layout_gravity = "center"
/>
<Textview
Android: Id = "@ + ID/text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: textcolor = "# ffffffff"
/>
</Linearlayout>

In this way, our drawer is complete. Let's see the effect.

 

 

So much. The drawer control is very practical. In addition to the application I mentioned at the beginning in the program, there are a lot of uses. using your imagination, the drawer will increase the color of your application.

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.