android实现侧边导航栏

1.新建项目时将最低jdk版本调到14,这是为了防止support-v7这个东西出现,这个东西出现真是祸害无穷
2.新建完项目,找到android-support-v4.jar这个文件,导入到libs目录下,并加入构建路径中。
3.使用android.support.v4.widget.DrawerLayout这个类来做布局,代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!-- 这是主界面的定义,即在不出现侧边栏之前的界面的定义 -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


    <!-- 下面是侧边栏的定义 -->
    <ListView android:id="@+id/left_drawer"  
        android:layout_width="150dp"  
        android:layout_height="match_parent"  
        android:layout_gravity="start"  
        android:choiceMode="singleChoice"  
        android:divider="#5F6F6C"  
        android:dividerHeight="1dp"  
        android:background="#8BF0DE"/> 
     <!-- 其中的layout_gravity属性指定是从哪个方向滑动手指会出现侧边栏 -->
    
</android.support.v4.widget.DrawerLayout>
4.在主界面的Activity中,为ListView来添加菜单项:
package com.example.navigation;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity {


private ListView navigationBarListView;
private String[] itemArray = new String[]{"张三","李四","王五","钱六","赵七"};

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.navigation_item, itemArray);
//这儿要使用R.layout.navigation_item,而不是R.id.navigation_item

navigationBarListView = (ListView) findViewById(R.id.left_drawer);
navigationBarListView.setAdapter(adapter);

}


}


5.这样,一个简单的侧边栏就诞生啦。如果自己有其他的需求,只需要相应的做少许改动即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值