安卓開發SlidingDrawer實現抽屜效果

來源:互聯網
上載者:User

在手機開發中,我們會遇到這樣的問題,要在手機螢幕上顯示很多資訊,但是手機螢幕就那麼小一點,當內容較多的時候如何顯示呢,我們如何理用更有限的空間來顯示更多的資訊呢?我們可以使用安卓系統提供的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;
       }
    }
}

運行即可看到效果.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.