android DrawerLayout侧滑菜单实现

在网上看到很多实现侧滑菜单的例子,但是都比较麻烦。 也找了一个比较简单的实现,用到DrawerLayout。只需要在xml文件中配置好,在Activity中就可以直接使用了。

xml文件如下

<RelativeLayout 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" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- The main content view -->

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button
                android:id="@+id/btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="open" />

            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </FrameLayout>

        <!-- The left drawer -->

        <ListView
           android:id="@+id/left_list"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>
Activity如下

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView left_list;
    private TextView text;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		left_list = (ListView)this.findViewById(R.id.left_list);
		text = (TextView)this.findViewById(R.id.text);
        List<String> list = new ArrayList<String>();
        list.add("abcedfg");
        list.add("hijklmn");
        list.add("opqrst");
        list.add("uvwxyz");
        left_list.setAdapter(new ArrayAdapter<>(left_list.getContext(), android.R.layout.simple_list_item_1, list));
        left_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				text.setText("hello" + arg2);
				System.out.println("hello");
			}
		});
        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });
	}
}
这样,就能很简单的实现侧滑菜单的效果了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值