android之抽屜布局應用

來源:互聯網
上載者:User

我們有時後希望在一個介面中顯示另外一個布局,但是又要不需要佔用太多布局空間,這個時候我們可以考慮用到抽屜布局,這個名詞大家可以充分發揮能動想象,相信通過下面的介紹大家一定可以對這種方式有所瞭解.

好了,直接上一個簡單的小項目.

1、首先我們建一個主activity

package com.jindegege.activity;import com.jindegege.service.MyAdapter;import android.app.Activity;import android.os.Bundle;import android.widget.GridView;import android.widget.ImageView;import android.widget.SlidingDrawer;public class SlidingdrawerActivity extends Activity {   private GridView gridview;  private SlidingDrawer slidingdrawer;  private ImageView imageview;  private int[] icons={R.drawable.main1,R.drawable.main2,                        R.drawable.main3,R.drawable.main4,                        R.drawable.main5,R.drawable.main6,                        R.drawable.main7,R.drawable.main8,R.drawable.main9};  private String[] items={"華仔","發哥","雅芝","柏芝","周星星","jindegege","老毛","老畢","涵涵"};         /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        gridview = (GridView)findViewById(R.id.gridview);         slidingdrawer = (SlidingDrawer)findViewById(R.id.sd);        imageview=(ImageView)findViewById(R.id.imageview);        MyAdapter adapter=new MyAdapter(this,items,icons);//通過建構函式執行個體化一個MyAdapter對象,這個MyAdapter對象必須繼承BaseAdapter類        gridview.setAdapter(adapter);        slidingdrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()//開啟抽屜        {          @Override          public void onDrawerOpened()          {          imageview.setImageResource(R.drawable.photo);//開啟抽屜事件           }        });        slidingdrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener()        {          @Override          public void onDrawerClosed()          {          imageview.setImageResource(R.drawable.ic_launcher);//關閉抽屜事件          }        });    }}

2、建立這個主activity要載入的布局檔案

<?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="fill_parent"     android:layout_height="fill_parent"     android:handle="@+id/imageview"     android:content="@+id/gridview"     android:orientation="vertical"  >      <ImageView        android:id="@id/imageview"        android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/ic_launcher"      />      <GridView       android:id="@id/gridview"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:numColumns="3"      android:background="#EE82EE"       android:gravity="center"    />           </SlidingDrawer></RelativeLayout>

第三步:在主activity中,我們要通過建構函式執行個體化一個MyAdapter對象,這個MyAdapter對象必須繼承BaseAdapter類,

以用來自訂一個適配器,先建立一個xml檔案item.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">  <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="#000000"  /></LinearLayout>

第四步:這個時候就是設計我們自訂的類了。

package com.jindegege.service;import com.jindegege.activity.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyAdapter extends BaseAdapter {   private Context context;  private String[] items;  private int[] icons;  public MyAdapter(Context context,String[] items,int[] icons) //構造器  {    this.context=context;    this.items=items;    this.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(context);    View v = (View) factory.inflate(R.layout.item, null);//綁定自訂的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;  } }

好的,就不給大家貼出了,大家可以自己下載該小項目,看看效果,自己也可以修改實現更好的效果。

原始碼:http://download.csdn.net/detail/jindegegesun/4086564

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.