android之抽屜布局應用
我們有時后希望在一個界面中顯示另外一個布局,但是又要不需要占用太多布局空間,這個時候我們可以考慮用到抽屜布局,這個名詞大家可以充分發揮能動想象,相信通過下面的介紹大家一定可以對這種方式有所了解.
好了,直接上一個簡單的小項目.
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
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!