参考文章:

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html

http://www.codeceo.com/article/android-toolbar-develop.html

http://blog.csdn.net/jdsjlzx/article/details/41441083/

http://www.open-open.com/lib/view/open1431356199216.html

http://blog.csdn.net/elinavampire/article/details/41477525

首先要写好toolbar的layout,方便以后include

my_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"> </android.support.v7.widget.Toolbar>

my_drawerlayout.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" > <FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout> <RelativeLayout
android:id="@+id/left"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white"> <ListView
android:id="@+id/left_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout> <RelativeLayout
android:id="@+id/right"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@android:color/holo_green_light"> <TextView
android:id="@+id/right_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="个人登陆页面" />
</RelativeLayout> </android.support.v4.widget.DrawerLayout>

然后在主layout里include

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<!--Toolbar-->
<include layout="@layout/my_toolbar" />
<!--DrawerLayout-->
<include layout="@layout/my_drawerlayout" />
</LinearLayout>

因为Drawerlayout里加了listview,要给listview写adapter,用来传入listview里的内容

因为我们想传入一个图片,一个字符串,所以写个model类

ContentModel.java

public class ContentModel {
private int imageView;
private String text; public ContentModel(int imageView, String text) {
super();
this.imageView = imageView;
this.text = text;
} public int getImageView() {
return imageView;
} public void setImageView(int imageView) {
this.imageView = imageView;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
}
}

ContentAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; import java.util.List; /**
* Created by Administrator on 2016/4/20 0020.
*/
public class ContentAdapter extends BaseAdapter
{
private Context context;
private List<ContentModel> list; public ContentAdapter(Context context, List<ContentModel> list) {
super();
this.context = context;
this.list = list;
} @Override
public int getCount() {
if (list != null) {
return list.size();
}
return 0;
} @Override
public Object getItem(int position) {
if (list != null) {
return list.get(position);
}
return null;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHold hold;
if (convertView == null) {
hold = new ViewHold();
convertView = LayoutInflater.from(context).inflate(
R.layout.content_item, null);
convertView.setTag(hold);
}else {
hold=(ViewHold) convertView.getTag();
} hold.imageView=(ImageView) convertView.findViewById(R.id.item_imageview);
hold.textView=(TextView) convertView.findViewById(R.id.item_textview); hold.imageView.setImageResource(list.get(position).getImageView());
hold.textView.setText(list.get(position).getText());
return convertView;
} static class ViewHold {
public ImageView imageView;
public TextView textView;
}
}

最后在MainActivity里初始化

public class MainActivity extends Activity {

    private DrawerLayout drawerLayout;
private RelativeLayout leftLayout;
private RelativeLayout rightLayout;
private List<ContentModel> list;
private ContentAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
leftLayout=(RelativeLayout) findViewById(R.id.left);
rightLayout=(RelativeLayout) findViewById(R.id.right);
ListView listView=(ListView) leftLayout.findViewById(R.id.left_listview); initData();
adapter=new ContentAdapter(this, list);
listView.setAdapter(adapter);
} private void initData() {
list=new ArrayList<ContentModel>(); list.add(new ContentModel(R.drawable.doctoradvice2, "新闻"));
list.add(new ContentModel(R.drawable.infusion_selected, "订阅"));
list.add(new ContentModel(R.drawable.mypatient_selected, "图片"));
list.add(new ContentModel(R.drawable.mywork_selected, "视频"));
list.add(new ContentModel(R.drawable.nursingcareplan2, "跟帖"));
list.add(new ContentModel(R.drawable.personal_selected, "投票"));
} }

Toolbar和Drawerlayout的基本使用的更多相关文章

  1. ToolBar和DrawerLayout的使用实现侧拉栏抽屉的开闭

    1.如图可以看到textColorPrimary,colorPrimary,colorPrimaryDark,navigationBarColor等颜色属性代表的相应位置,如下图 2.具体属性在res ...

  2. android ToolBar与DrawerLayout笔记

    通过Android Studio 生成的Nagvition DrawerLayout Activity 自带的布局中的NagvitionView会覆盖ToolBar直接通到statusBar. 但是自 ...

  3. 安卓Design包之NavigationView结合DrawerLayout,toolbar的使用,FloatingActionButton

    注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头 FloatingActionButton 悬浮按钮:FloatingActionButton是重写ImageView的,所有 ...

  4. DrawerLayout和toolbar的使用

    onPostCreate()是Activity完全启动后的调用:在完全启动后的回调设置toolbar 然后在使用 AppCompatActivity 时style要设置为何appCompat相关的样式 ...

  5. Android之官方导航栏之Toolbar(Toolbar+DrawerLayout+ViewPager+PagerSlidingTabStrip)

    通过前几篇文章,我们对Android的导航栏有了一定的了解认识,本次文章将对Toolbar进行综合应用,主要结合DrawerLayout.ViewPager.PagerSlidingTabStrip一 ...

  6. Android——使用Toolbar + DrawerLayout快速实现高大上菜单侧滑(转)

    今天就来使用官方支持库来快速实现这类效果,需要使用到Toolbar和DrawerLayout,详细步骤如下:(如果你还不知道这两个Widget,先自己Google吧~) 1.首先需要添加appcomp ...

  7. android 5.X Toolbar+DrawerLayout实现抽屉菜单

    前言  android5.X新增的一个控件Toolbar,这个控件比ActionBar更加自由,可控,因为曾经的ActionBar的灵活性比較差,所以google逐渐使用Toolbar替代Action ...

  8. Android ToolBar自定义图标,关联DrawerLayout

    Android5.0出现了一个可以代替ActionBar的控件ToolBar,使用更加灵活,一般我们使用ToolBar来和DrawerLayout配合使用,官方提供了一个开关类ActionBarDra ...

  9. Material Design控件使用学习 toolbar+drawerlayout+ Snackbar

    效果 1.,导包design包和appcompat-v7 ,设置Theme主题Style为NoActionbar 2.custom_toolbar.xml <?xml version=" ...

随机推荐

  1. 6,synchronized, lock 区别

    参考文档 http://zzhonghe.iteye.com/blog/826162 http://houlinyan.iteye.com/blog/1112535 1,ReentrantLock 拥 ...

  2. css3中的translate,transform,transition的区别

    translate:移动,transform的一个方法               通过 translate() 方法,元素从其当前位置移动,根据给定的 left(x 坐标) 和 top(y 坐标) ...

  3. unix高级编程阅读

    一.进程: fork,exec,waitpid 1.子进程:复制进程的代码与堆栈状态,因此子进程将会从刚执行的指令fork位置继续往下执行. 2.父进程通过waitpid等待子进程完成 二.线程: 1 ...

  4. 搭建gogs

    https://blog.csdn.net/hwm_life/article/details/82969005 服务器环境 CentOS 7 64位 安装Gogs所需的其它环境 需要安装的依赖有Ngi ...

  5. Vue项目中用的Jquery.js和easing.js做的抛物线

    如果项目中还用到自有的$ <script> jQuery.noConflict();</script> //星星掉落movePathNew(args) { let self = ...

  6. C++ 流控制函数setw()、setfill()、setbase()、setprecision()的使用

    头文件: #include <iostream> #include <iomanip> 功能: std::setw :需要填充多少个字符,默认填充的字符为' '空格 std:: ...

  7. npm run build报错 ,resolve is not defined

    今天在build项目的时候报: ReferenceError: resolve is not defined npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ...

  8. 浅谈Supermap iClient for JavaScript 弹窗类

    地图作为信息的载体和呈现方式,是GIS的重要组成部分,它是一个浏览信息的窗口,在信息日益发达的今天 ,各种地图应用如雨后春笋一般出现在大众眼前,而不是像以往一样太过局限于专业的领域.而弹窗,是作为地图 ...

  9. EntityFrameWork Code First 一对多关系处理

    场景1: 一个文章类别(Category)下含有多篇文章(Article),而某篇文章只能对应一个类别 Article和Category的代码如下: /// <summary> /// 文 ...

  10. Android ListView中EditView再次焦点获取

    问题:在ListView中使用EditView,当第一次将焦点给到EditView的时候弹出小键盘.使得EditView失去焦点. 分析:因为在第一次使用EditView弹出小键盘之后,会重新的调用一 ...