在手機開發中,我們會遇到這樣的問題,要在手機螢幕上顯示很多資訊,但是手機螢幕就那麼小一點,當內容較多的時候如何顯示呢,我們如何理用更有限的空間來顯示更多的資訊呢?我們可以使用安卓系統提供的SlidingDrawer類,使用SlidingDrawer類我們就可以藉助SlidingDrawer實現抽屜效果,這就是傳說中的安卓抽屜效果,下面看參考代碼,以下代碼經本站親測,讀者可以拷貝到Eclipse項目中運行。下面直接貼出代碼。
首先看一下運行效果:
開始建立項目,首先準備幾張圖片,如下:
1.handle_normal.png
下載
2.handle_focused.png
下載
3.handle_pressed.png
下載
4.list_selector_background_pressed.png
下載
以片均放置在drawable-hdpi/drawable-ldpi/drawable-mdpi/drawable-xhdpi中。
然後我們來看代碼如何寫:
1.在es/drawable目錄下定義一個資源檔handle.xml,參考代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=http://schemas.android.com/apk/res/android>
<item android:state_window_focused="false" android:drawable="@drawable/handle_normal" />
<item android:state_focused="true" android:drawable="@drawable/handle_focused" />
<item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />
</selector>
2.在es/drawable目錄下定義一個資源檔listview_selected.xml,參考代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=http://schemas.android.com/apk/res/android>
<item android:state_pressed="true"
android:drawable="@drawable/list_selector_background_pressed" />
</selector>
3.main.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">
<SlidingDrawer
android:id="@+id/slidingdrawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:handle="@+id/handle"
android:content="@+id/content">
<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/handle" />
<ListView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</SlidingDrawer>
</LinearLayout>
4.在res/layout檔案家中建立listview_layout.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"
android:background="#ffffff" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/listview_selected"
android:padding="6px">
<TextView
android:id="@+id/webName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px"
android:textColor="#000000"/>
<TextView
android:id="@+id/webDescript"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16px"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
5.對應java檔案(本例是AndroidSlidingDrawerActivity.java)的參考代碼:
public class AndroidSlidingDrawerActivity extends Activity {
private ListView myListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews(){
myListView = (ListView)findViewById(R.id.content);
myListView.setAdapter(new ListViewAdapter());
}
private class ListViewAdapter extends BaseAdapter{
//這裡返回10行,ListView有多少行取決於getCount()方法
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
if(v == null){
v = inflater.inflate(R.layout.listview_layout, null);
}
TextView myWebName = (TextView)v.findViewById(R.id.webName);
TextView myWebDescript = (TextView)v.findViewById(R.id.webDescript);
myWebName.setText("Android開發學習網" + position);
myWebDescript.setText("slidingdrawer用法詳解" + position);
return v;
}
}
}
運行即可看到效果.