zoukankan      html  css  js  c++  java
  • Toolbar和Drawerlayout的基本使用

    参考文章:

    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, "投票"));
        }
    
    }
  • 相关阅读:
    c# 事件阻断
    正则语义化API
    c# 防止继承和单例
    Maxscript 控制流混淆
    3dmax快速安装补丁的方法
    c# 使用类中的方法更新自己
    Maxscript 变量作用域
    Maxscript 键值对
    Maxscript 数据结构和算法记录
    Datawhale 之NLP学习-打卡(五)
  • 原文地址:https://www.cnblogs.com/qlky/p/5412304.html
Copyright © 2011-2022 走看看