码迷,mamicode.com
首页 > 其他好文 > 详细

侧拉菜单

时间:2016-04-26 17:22:30      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

第一种:SlidingPaneLayout

<android.support.v4.widget.SlidingPaneLayout
    android:id="@+id/sliding_pane_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.mobiletrain.a7_2slidingpanelayout.MainActivity">
    <!--
    SlidingPaneLayout布局整体上分为两部分,第一部分为左边的侧拉菜单,第二部分为主页面
    -->
    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:background="#f90101"
        android:gravity="center"
        android:orientation="vertical">

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#08f504"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"></android.support.v7.widget.Toolbar>

        <Button
            android:id="@+id/toggle"
            android:layout_width="wrap_content"
            android:onClick="toggle"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="主页面"/>
    </LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
public class MainActivity extends AppCompatActivity {

    private List<String> list;
    private SlidingPaneLayout slidingPaneLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ListView listView = (ListView) findViewById(R.id.lv);
        final TextView textView = (TextView) findViewById(R.id.tv);
        LinearLayout leftLayout = (LinearLayout) findViewById(R.id.left_layout);
        ViewGroup.LayoutParams layoutParams = leftLayout.getLayoutParams();
        //将100px转为100dp
        layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
        leftLayout.setLayoutParams(layoutParams);
        slidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.sliding_pane_layout);
        initData();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                textView.setText(list.get(position));
                //关闭侧拉菜单
                slidingPaneLayout.closePane();
            }
        });
    }

    //点击后退按钮
    @Override
    public void onBackPressed() {
        //如果侧拉菜单打开,则点击后退按钮时关闭侧拉菜单,否则直接退出应用
        if (slidingPaneLayout.isOpen()) {
            slidingPaneLayout.closePane();
        } else {
            super.onBackPressed();
        }
    }

    private void initData() {
        list = new ArrayList<>();
        list.add("收藏");
        list.add("钱包");
        list.add("相册");
        list.add("文件");
    }

    public void toggle(View view) {
        //点击按钮时,如果侧拉菜单已经打开,则关闭,否则打开侧拉菜单
        if (slidingPaneLayout.isOpen()) {
            slidingPaneLayout.closePane();
        } else {
            slidingPaneLayout.openPane();
        }
    }
}

 第二种:DrawerLayout

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.mobiletrain.a7_2drawerlayout.MainActivity">
    <!--
    DrawerLayout中整体上分为三大块,分别是主页面,左右两侧的侧拉菜单
    android:layout_gravity="left"属性表示该页面是左边的侧拉菜单,注意要给侧拉菜单的布局添加一个背景
    android:layout_gravity="right"属性表示该页面是右边侧拉菜单

    注意三部分的位置:
    第一部分为主界面
    第二部分为左边侧拉菜单
    第三部分为右边侧拉菜单
    如果侧拉菜单写在了主页面之前,则侧拉菜单中的点击事件会失效
    -->
    <RelativeLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="leftToggle"
            android:text="leftToggle"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="rightToggle"
            android:text="@string/righttoggle"/>

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="主页面"/>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#00fa08"
        android:orientation="vertical">

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#fa0212"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="right"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ListView listView = (ListView) findViewById(R.id.lv);
        final TextView textView = (TextView) findViewById(R.id.tv);
        initData();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                textView.setText(list.get(position));
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.closeDrawer(Gravity.LEFT);
        } else if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            drawerLayout.closeDrawer(Gravity.RIGHT);
        } else {
            super.onBackPressed();
        }
    }

    private void initData() {
        list = new ArrayList<>();
        list.add("收藏");
        list.add("钱包");
        list.add("文件");
        list.add("相册");
    }

    public void leftToggle(View view) {
        //判断左边的侧拉菜单是否打开
        if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            //关闭掉左边的侧拉菜单
            drawerLayout.closeDrawer(Gravity.LEFT);
        } else {
            //打开左边的侧拉菜单
            drawerLayout.openDrawer(Gravity.LEFT);
        }
    }

    public void rightToggle(View view) {
        //打开右边的侧拉菜单
        drawerLayout.openDrawer(Gravity.RIGHT);
    }
}

 

侧拉菜单

标签:

原文地址:http://www.cnblogs.com/anni-qianqian/p/5435713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!