参考文章:

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. SP16580 QTREE7 - Query on a tree VII(LCT)

    题意翻译 一棵树,每个点初始有个点权和颜色(输入会给你) 0 u:询问所有u,v路径上的最大点权,要满足u,v路径上所有点颜色相同 1 u:反转u的颜色 2 u w:把u的点权改成w 题解 Qtree ...

  2. exec和xargs

    参考:http://www.cnblogs.com/itxdm/p/5936907.html 一. 先复习下find命令 1. name参数 find -name tom 或 find -iname ...

  3. 什么是cluster(群集)

    一.群集的概念 在互联网应用中,随着站点对硬件性能.相应速度.服务稳定性.数据可靠性等要求越来越高,单台服务器力不从心,使用小型机或大型机价格还太昂贵,因此我们使用普通服务器来构建服务群集显然最划算. ...

  4. c#中的classes和objects一些知识【1】

    首先我们需要知道面向对象语言(Object-oriented language)的三大特点:封装(Encapulation),继承(Inheritance),多态(Polymorphism). 引言: ...

  5. iOS hook原理

    OC中的method其实是一个结构体 struct objc_method{ SEL method_name char *method_types IMP method_imp } SEL是方法名,I ...

  6. CH2601 电路维修(双端队列bfs)建图恶心

    CH2601 电路维修 双端队列bfs,其实就是因为只有0和1所以可以直接2维护队列单调性(和优先队列一个道理) 建图的过程需要仔细斟酌(想一想id为什么这么写) 还有,空间要开够(很玄学),我一开始 ...

  7. 使用canvas实现画中画效果的H5

    最近看到一个挺有趣的H5,主要效果就是通过不断的放缩来展示画中画,网上找了一下并没有这方面的实现代码,故决定原创一下,并分享出来 主要的思路就是通过canvas不断的写入图片,考虑到每一层的图片的位置 ...

  8. 【手记】如果Idx/Sub字幕导不进MKVToolNix,看看是否这个原因

    用记事本之类的文本编辑器打开idx文件,看看时间轴部分是不是存在不规范的条目,比如: timestamp: :::, filepos: 注意,上述条目中,filepos:后面缺了一个空格,就这么一处问 ...

  9. springcloud(三)-Eureka

    Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...

  10. Oracle知识转储

    https://blog.csdn.net/u011479200/article/details/53086411 https://www.cnblogs.com/LiYi-Dao/p/9406189 ...