zoukankan      html  css  js  c++  java
  • drawableLayout的使用(转载讲的比较清晰的文章)

    创建drawbler的布局文件
    初始化drawbler的列表
    响应drawable列表点击事件


    现在侧滑菜单使用很多,大都是通过SlidingMenu实现。现在也可以通过DrawerLayout

    创建抽屉布局


    frament_content.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <TextView  
    8.         android:id="@+id/textView"  
    9.         android:layout_width="match_parent"  
    10.         android:layout_height="wrap_content"  
    11.         android:textSize="25sp" />  
    12.   
    13. </LinearLayout>  
    activity_main.xml

    1. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     android:id="@+id/drawer_layout"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent" >  
    5.   
    6.     <!-- The main content view -->  
    7.   
    8.     <FrameLayout  
    9.         android:id="@+id/content_frame"  
    10.         android:layout_width="match_parent"  
    11.         android:layout_height="match_parent" >  
    12.     </FrameLayout>  
    13.   
    14.     <!-- The navigation view -->  
    15.   
    16.     <ListView  
    17.         android:id="@+id/left_drawer"  
    18.         android:layout_width="240dp"  
    19.         android:layout_height="match_parent"  
    20.         android:layout_gravity="start"  
    21.         android:background="#ffffcc"  
    22.         android:choiceMode="singleChoice"  
    23.         android:divider="@android:color/transparent"  
    24.         android:dividerHeight="0dp" >  
    25.     </ListView>  
    26.   
    27. </android.support.v4.widget.DrawerLayout>  

    然后新建一个类继承Fragment类

    1. /** 
    2.  * ContentFragment.java 
    3.  * 版权所有(C) 2015 
    4.  * 创建者:cuiran 2015-1-3 下午3:25:44 
    5.  */  
    6. package com.cayden.drawerlayoutdemo;  
    7.   
    8. import android.app.Fragment;  
    9. import android.os.Bundle;  
    10. import android.view.LayoutInflater;  
    11. import android.view.View;  
    12. import android.view.ViewGroup;  
    13. import android.widget.TextView;  
    14.   
    15. /** 
    16.  * TODO  
    17.  * @author cuiran 
    18.  * @version 1.0.0 
    19.  */  
    20. public class ContentFragment extends Fragment {  
    21.   
    22.     private TextView textView;  
    23.       
    24.     @Override  
    25.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
    26.             Bundle savedInstanceState) {  
    27.         View view = inflater.inflate(R.layout.fragment_content, container, false);  
    28.         textView = (TextView) view.findViewById(R.id.textView);  
    29.           
    30.         String text = getArguments().getString("text");  
    31.         textView.setText(text);  
    32.           
    33.         return view;  
    34.     }  
    35.       
    36. }  

    完成Activity代码

    1. package com.cayden.drawerlayoutdemo;  
    2.   
    3. import java.util.ArrayList;  
    4.   
    5. import android.app.Activity;  
    6. import android.app.Fragment;  
    7. import android.app.FragmentManager;  
    8. import android.content.Intent;  
    9. import android.content.res.Configuration;  
    10. import android.net.Uri;  
    11. import android.os.Bundle;  
    12. import android.support.v4.app.ActionBarDrawerToggle;  
    13. import android.support.v4.widget.DrawerLayout;  
    14. import android.view.Menu;  
    15. import android.view.MenuItem;  
    16. import android.view.View;  
    17. import android.widget.AdapterView;  
    18. import android.widget.AdapterView.OnItemClickListener;  
    19. import android.widget.ArrayAdapter;  
    20. import android.widget.ListView;  
    21.   
    22.   
    23. public class MainActivity extends Activity implements OnItemClickListener {  
    24.   
    25.     private DrawerLayout mDrawerLayout;  
    26.     private ListView mDrawerList;  
    27.     private ArrayList<String> menuLists;  
    28.     private ArrayAdapter<String> adapter;  
    29.     private ActionBarDrawerToggle mDrawerToggle;  
    30.     private String mTitle;  
    31.   
    32.     @Override  
    33.     protected void onCreate(Bundle savedInstanceState) {  
    34.         super.onCreate(savedInstanceState);  
    35.         setContentView(R.layout.activity_main);  
    36.   
    37.         mTitle = (String) getTitle();  
    38.   
    39.         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
    40.         mDrawerList = (ListView) findViewById(R.id.left_drawer);  
    41.         menuLists = new ArrayList<String>();  
    42.         for (int i = 0; i < 5; i++)  
    43.             menuLists.add("菜单0" + i);  
    44.         adapter = new ArrayAdapter<String>(this,  
    45.                 android.R.layout.simple_list_item_1, menuLists);  
    46.         mDrawerList.setAdapter(adapter);  
    47.         mDrawerList.setOnItemClickListener(this);  
    48.   
    49.         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,  
    50.                 R.drawable.ic_drawer, R.string.drawer_open,  
    51.                 R.string.drawer_close) {  
    52.             @Override  
    53.             public void onDrawerOpened(View drawerView) {  
    54.                 super.onDrawerOpened(drawerView);  
    55.                 getActionBar().setTitle("请选择");  
    56.                 invalidateOptionsMenu(); // Call onPrepareOptionsMenu()  
    57.             }  
    58.   
    59.             @Override  
    60.             public void onDrawerClosed(View drawerView) {  
    61.                 super.onDrawerClosed(drawerView);  
    62.                 getActionBar().setTitle(mTitle);  
    63.                 invalidateOptionsMenu();  
    64.             }  
    65.         };  
    66.         mDrawerLayout.setDrawerListener(mDrawerToggle);  
    67.           
    68.         //开启ActionBar上APP ICON的功能  
    69.         getActionBar().setDisplayHomeAsUpEnabled(true);  
    70.         getActionBar().setHomeButtonEnabled(true);  
    71.   
    72.     }  
    73.   
    74.     @Override  
    75.     public boolean onPrepareOptionsMenu(Menu menu) {  
    76.         boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);  
    77.         menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);  
    78.         return super.onPrepareOptionsMenu(menu);  
    79.     }  
    80.   
    81.     @Override  
    82.     public boolean onCreateOptionsMenu(Menu menu) {  
    83.         // Inflate the menu; this adds items to the action bar if it is present.  
    84.         getMenuInflater().inflate(R.menu.main, menu);  
    85.         return true;  
    86.     }  
    87.   
    88.     @Override  
    89.     public boolean onOptionsItemSelected(MenuItem item) {  
    90.         //将ActionBar上的图标与Drawer结合起来  
    91.         if (mDrawerToggle.onOptionsItemSelected(item)){  
    92.             return true;  
    93.         }  
    94.         switch (item.getItemId()) {  
    95.         case R.id.action_websearch:  
    96.             Intent intent = new Intent();  
    97.             intent.setAction("android.intent.action.VIEW");  
    98.             Uri uri = Uri.parse("http://www.baidu.com");  
    99.             intent.setData(uri);  
    100.             startActivity(intent);  
    101.             break;  
    102.         }  
    103.         return super.onOptionsItemSelected(item);  
    104.     }  
    105.       
    106.     @Override  
    107.     protected void onPostCreate(Bundle savedInstanceState) {  
    108.         super.onPostCreate(savedInstanceState);  
    109.         //需要将ActionDrawerToggle与DrawerLayout的状态同步  
    110.         //将ActionBarDrawerToggle中的drawer图标,设置为ActionBar中的Home-Button的Icon  
    111.         mDrawerToggle.syncState();  
    112.     }  
    113.       
    114.     @Override  
    115.     public void onConfigurationChanged(Configuration newConfig) {  
    116.         super.onConfigurationChanged(newConfig);  
    117.         mDrawerToggle.onConfigurationChanged(newConfig);  
    118.     }  
    119.       
    120.       
    121.       
    122.       
    123.       
    124.       
    125.       
    126.   
    127.     @Override  
    128.     public void onItemClick(AdapterView<?> arg0, View arg1, int position,  
    129.             long arg3) {  
    130.         // 动态插入一个Fragment到FrameLayout当中  
    131.         Fragment contentFragment = new ContentFragment();  
    132.         Bundle args = new Bundle();  
    133.         args.putString("text", menuLists.get(position));  
    134.         contentFragment.setArguments(args);  
    135.   
    136.         FragmentManager fm = getFragmentManager();  
    137.         fm.beginTransaction().replace(R.id.content_frame, contentFragment)  
    138.                 .commit();  
    139.   
    140.         mDrawerLayout.closeDrawer(mDrawerList);  
    141.     }  
    142.   
    143. }  
       




  • 相关阅读:
    [HAOI2018]苹果树
    [TJOI2013]拯救小矮人
    [SDOI2016]硬币游戏
    一辈子都学不会的有上下界的网络流
    [AHOI2014/JSOI2014]支线剧情
    [JSOI2009]球队收益
    hdu-1856 More is better---带权并查集
    hdu-1325 Is It A Tree?---并查集
    hdu-1272 小希的迷宫---并查集或者DFS
    hdu1213-How Many Tables---基础并查集
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/6c7d85e7999c99cd9a12a5d8105f7bdc.html
Copyright © 2011-2022 走看看