Android 抽屜布局

來源:互聯網
上載者:User

標籤:

  目前部分APP使用一種類似抽屜式的布局,像QQ那種,感覺很炫,自己也一直想做一個像那樣的布局,(ps網上很多這樣的例子,我下面做的就是參考網上的改變的)

  廢話不就不說了,直接上代碼

  1、首先建立一個布局檔案activity_drawer.xml 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:paddingBottom="@dimen/activity_vertical_margin" 6     android:paddingLeft="@dimen/activity_horizontal_margin" 7     android:paddingRight="@dimen/activity_horizontal_margin" 8     android:paddingTop="@dimen/activity_vertical_margin" 9     tools:context=".DrawerActivity">10     <android.support.v4.widget.DrawerLayout<!--重點在這裡-->11         android:id="@+id/drawer_layout"12         android:layout_width="match_parent"13         android:layout_height="match_parent">14             <FrameLayout15                 android:id="@+id/content_frame"16                 android:layout_width="match_parent"17                 android:layout_height="match_parent">18                 <Button19                     android:id="@+id/btn"20                     android:layout_width="wrap_content"21                     android:layout_height="wrap_content"22                     android:text="開啟"23                     android:layout_alignParentTop="true"24                     android:layout_alignParentLeft="true"25                     android:layout_alignParentStart="true"26                     android:layout_alignParentRight="true"27                     android:layout_alignParentEnd="true" />28             </FrameLayout>29             <ListView30                 android:id="@+id/left_drawer"31                 android:layout_width="200dp"32                 android:layout_height="match_parent"33                 android:layout_gravity="start"34                 android:background="#ff349710"35                 android:choiceMode="singleChoice"36                 android:dividerHeight="0dp"37                 android:layout_marginTop="30dp"38                />39     </android.support.v4.widget.DrawerLayout>40 </RelativeLayout>

  2.對應的Activity檔案DrawerActivity.java

 1 public class DrawerActivity extends Activity { 2     public static final String[] str = new String[]{"東部", "西部", "南部", "北部", "中部", 3             "東南部", "西北部", "西南部", "東北部", "中西部", "中南部", "中北部"}; 4     private DrawerLayout mDrawerLayout = null; 5     private ListView mlistView; 6     @Override 7     protected void onCreate(Bundle savedInstanceState) { 8         super.onCreate(savedInstanceState); 9         setContentView(R.layout.activity_drawer);10 11         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);12         mlistView = (ListView) findViewById(R.id.left_drawer);13 14         15         //下面兩種方式都可以,後者要多建立一個value的array檔案16         mlistView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str));17         //mlistView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.planets_array)));18         Button button = (Button) findViewById(R.id.btn);19         button.setOnClickListener(new View.OnClickListener() {20 21             @Override22             public void onClick(View v) {23                 // 按鈕按下,將抽屜開啟24                 mDrawerLayout.openDrawer(Gravity.LEFT);25                 //mDrawerLayout.closeDrawers();//可以不監聽,預設點擊其他地方即可關閉26                 //adapter.notifyDataSetChanged();27             }28         });29         //清單控制項監聽30         mlistView.setOnItemClickListener(new DrawerItemClickListener());31     }32 33     private class DrawerItemClickListener implements ListView.OnItemClickListener {34         @Override35         public void onItemClick(AdapterView parent, View view, int position, long id) {36             selectItem(position);37         }38     }39     //簡單輸出一些資訊40     private void selectItem(int position) {41         new AlertDialog.Builder(this)42                 .setTitle("開啟")43                 .setMessage("開啟的位置是:"+position+"\n位置的值是:"+str[position])44                 .setPositiveButton("確定", new DialogInterface.OnClickListener() {45                     @Override46                     public void onClick(DialogInterface dialogInterface, int i) {47                     //監聽事件48                     }49                 }).show();50     }51 }

  3.簡單效果如下:

 

  這僅僅是實現了效果,關於為什麼待我研究透徹了再分享。

Android 抽屜布局

聯繫我們

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