zoukankan      html  css  js  c++  java
  • 继承RelativeLayout 自定义布局

    public class HomeToolbarView extends RelativeLayout {
        TextView tvTitle;
    
        public HomeToolbarView(Context context) {
            super(context);
            addView(context);
        }
        public HomeToolbarView(Context context, AttributeSet attrs) {
            super(context, attrs);
            addView(context);
        }
        public HomeToolbarView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            addView(context);
        }
    
    
        public void addView(Context mContext){
            ImageView imageViewMenu=new ImageView(mContext);
            ImageView imageViewMessage=new ImageView(mContext);
            tvTitle=new TextView(mContext);
            addView(imageViewMenu);
            addView(imageViewMessage);
            addView(tvTitle);
    
            RelativeLayout.LayoutParams menuLayoutParams = (RelativeLayout.LayoutParams) imageViewMenu.getLayoutParams();
            RelativeLayout.LayoutParams messageLayoutParams = (RelativeLayout.LayoutParams) imageViewMessage.getLayoutParams();
            RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams) tvTitle.getLayoutParams();
    
            menuLayoutParams.width=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
            menuLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
            menuLayoutParams.setMargins(getResources().getDimensionPixelOffset(R.dimen.common_margin)
                    ,0,0,0);
            menuLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
            menuLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
    
    
            titleLayoutParams.width= LayoutParams.MATCH_PARENT;
            titleLayoutParams.height=LayoutParams.MATCH_PARENT;
            titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
    
            messageLayoutParams.width=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
            messageLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth);
            messageLayoutParams.setMargins(0
                    ,0,getResources().getDimensionPixelOffset(R.dimen.common_margin),0);
            messageLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
            messageLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
    
            imageViewMenu.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ico_toolbar_left_menu));
            imageViewMessage.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ico_toolbar_left_message));
    
            tvTitle.setText(mContext.getString(R.string.app_name));
            tvTitle.setTextSize(getResources().getDimension(R.dimen.textsize_8));
            tvTitle.setTextColor(ContextCompat.getColor(mContext,R.color.common_blue));
            tvTitle.setGravity(Gravity.CENTER);
            imageViewMenu.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    listener.leftClick();
                }
            });
            imageViewMessage.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    listener.rightClick();
                }
            });
    
            imageViewMenu.setLayoutParams(menuLayoutParams);
            imageViewMessage.setLayoutParams(messageLayoutParams);
            tvTitle.setLayoutParams(titleLayoutParams);
        }
    
        public void setTitle(String title){
            tvTitle.setText(title);
        }
    
        //自定义的顶部ActionBar的点击监听;
        private  HomeToolbarClickListener listener;
        //点击事件的监听接口
        public interface HomeToolbarClickListener {
             void leftClick();
             void rightClick();
        }
        //提供activity调用的方法,类似于Button类的setOnClickListener(OnClickListener listener)
        //传入具体实现方法
        public void setOnTopbarClickListener(HomeToolbarClickListener listener){
            this.listener=listener;
        }
    }
    View Code

    使用

       <com.freexiaoyu.app.widget.HomeToolbarView
            android:id="@id/toolbar_home"
            android:layout_width="match_parent"
            android:background="@color/white"
            android:layout_height="48.0dp"
            android:orientation="vertical"/>
    

      

    activity
    
    
        @BindView(R.id.toolbar_home)
        HomeToolbarView toolbar_home;
    
    
            toolbar_home.setTitle(getString(R.string.app_name));
    
     toolbar_home.setOnTopbarClickListener(new HomeToolbarView.HomeToolbarClickListener() {
                @Override
                public void leftClick() {
                    if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                    } else {
                        mDrawerLayout.openDrawer(Gravity.LEFT);
                    }
                }
    
                @Override
                public void rightClick() {
    
                }
            });

    展示效果

     

  • 相关阅读:
    (原创)智能电能表SM1算法开发套件(主站接口) 基础资料篇
    ora12500 tns 监听程序无法启动专用服务器进程
    老微开放下载地址了,Microsoft Visual Studio 2010 旗舰版试用版
    如何实现一套鼠标键盘控制二台主机
    (原创)智能电能表SM1算法开发套件(主站接口) 开发篇
    Win8消费者预览版各语言版本下载地址
    50个必备的实用jQuery代码段 DWZ富客户端
    DWZ富客户端框架设计思路与学习建议 DWZ富客户端
    Mysql嵌套集合模型【省份城市示例】 DWZ富客户端
    DWZRIA v1.3 RC1 发布 DWZ富客户端
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/9778527.html
Copyright © 2011-2022 走看看