参考文章:

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. Linux常用运维指令

    cd data/apps./=========================================== ps -ef | grep tomcatps -ef | grep desktopX ...

  2. springmvc初步配置

    导包/添加依赖: <dependency> <groupId>org.springframework</groupId> <artifactId>spr ...

  3. django中@property装饰器的运用

    python提供的内置装饰器——staticmethod.classmethod和property 在OSQA中,@property的使用频率是非常高的.下面就是它的使用方法: @property 可 ...

  4. 为 JSON 字符串创建对象

    ---------------------------页面效果---------------------------------- ---------------------------代码实现--- ...

  5. mysql 快照读MVCC

    mysql的读分快照读和当前读 快照读 是指写的同时,读不阻塞,达到并发的作用 这时候的读 是 记录的历史版本,存在于undo里,当然回滚时就的也是这个undo 当执行一条update语句时,记录本身 ...

  6. [转] kerberos介绍

    [From] https://www.cnblogs.com/ulysses-you/p/8107862.html 重要术语 1. KDC 全称:key distributed center 作用:整 ...

  7. hadoop job -kill 和 yarn application -kill 区别

    hadoop job -kill 调用的是CLI.java里面的job.killJob(); 这里会分几种情况,如果是能查询到状态是RUNNING的话,是直接向AppMaster发送kill请求的.Y ...

  8. 2.6 Go 读取CSV

    Go读取CSV文件,其内容被转换成字符串数组 package main import ( "encoding/csv" "fmt" "io/iouti ...

  9. Dubbo的Api+Provider+Customer示例(IDEA+Maven+Springboot+dubbo)

    项目结构 dubbo-demo dubbo-api:提供api接口,一般存储实体类和接口服务 dubbo-provider:dubbo生产者提供服务,一般存储接口具体实现 dubbo-customer ...

  10. golang笔记

    ----------- golang打包和部署到centos7. 参考:https://blog.csdn.net/qq_33230584/article/details/81536572