zoukankan      html  css  js  c++  java
  • Android公共title的应用

         我们在开发Android应用中,写每一个页面的时候都会建一个title,不是写一个LinearLayout就是写一个RelativeLayout,久而久之就会觉得这样繁琐,尤其几个页面是只是标题不一样,其他都相同的话,每个页面的title都重复搭建会显得代码冗余,而且增加渲染时间。于是我前段时间就写了一个公共title的activity,其他子Activity只要继承我这个就可以了。这段时间我们对项目代码进行了重构了,就利用了这个公共title(其他的同事进行了优化),确实少写了很多代码,用起来还是很不错的。  我知道网上也有类似的代码,但不是我想要的那种。废话不多说,直接上代码。以后还继续优化,在我眼里任何代码都能再优化一点点。

      1 public abstract class BaseTitleActivty extends Activity implements
      2         OnClickListener {
      3 
      4     /**
      5      * 标题头部的布局
      6      */
      7     protected ViewGroup title_layout;
      8 
      9     private LinearLayout mMianLayout;
     10     
     11     /**
     12      * 返回图标
     13      */
     14     protected ImageView iv_common_back;
     15 
     16     /**
     17      * 返回文字
     18      */
     19     protected TextView back_text;
     20 
     21     /**
     22      * 左边的图片
     23      */
     24     protected ImageView left_img;
     25 
     26     /**
     27      * 中间的标题
     28      */
     29     protected TextView title_text;
     30     /**
     31      * 右边的文字
     32      */
     33     protected TextView right_text;
     34     /**
     35      * 右边的图标
     36      */
     37     protected ImageView right_img;
     38 
     39     protected View base_title;
     40 
     41     @Override
     42     protected void onCreate(Bundle savedInstanceState) {
     43         super.onCreate(savedInstanceState);
     44         super.setContentView(R.layout.base_title_activity);
     45         initBaseTitle();
     46     }
     47 
     48     
     49 
     50     @Override
     51     public void setContentView(int layoutResID) {
     52         LayoutInflater inflater = (LayoutInflater) this
     53                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     54         View contentView = inflater.inflate(layoutResID, null);
     55         contentView.setLayoutParams(new FrameLayout.LayoutParams(
     56                 ViewGroup.LayoutParams.MATCH_PARENT,
     57                 ViewGroup.LayoutParams.MATCH_PARENT));
     58         title_layout.addView(contentView);
     59     }
     60 
     61     /**
     62      * 初始化方法
     63      */
     64     protected void initBaseTitle() {
     65         initTitleView();
     66         setTitle();
     67     }
     68 
     69     /**
     70      * 初始化公共title的view
     71      */
     72     protected void initTitleView() {
     73         title_layout =(ViewGroup)findViewById(R.id.title_layout);
     74         base_title = (View) findViewById(R.id.base_title);
     75         iv_common_back =(ImageView)findViewById(R.id.iv_common_back);                
     76         back_text = (TextView) findViewById(R.id.tv_back);
     77         left_img = (ImageView) findViewById(R.id.left_img);
     78         title_text = (TextView) findViewById(R.id.title);
     79         right_text = (TextView) findViewById(R.id.right_text);
     80         right_img = (ImageView) findViewById(R.id.right_img);
     81         iv_common_back.setOnClickListener(this);
     82         back_text.setOnClickListener(this);
     83         right_text.setOnClickListener(this);
     84         right_img.setOnClickListener(this);
     85     }
     86 
     87     /**
     88      * 设置标题内容
     89      */
     90     protected void setTitle() {
     91     }
     92 
     93     /**
     94      * 仅仅含返回图标
     95      */
     96     protected void showTitleLeftContent() {
     97         back_text.setText("");
     98     }
     99 
    100     /**
    101      * 仅仅含有返回图标和文字
    102      */
    103     protected void showTitleLeftContent(String tv_back) {
    104         back_text.setText(tv_back);
    105     }
    106 
    107     /**
    108      * 左边仅仅是一个图片
    109      */
    110     protected void showTitleLeftContent(int resId) {
    111         iv_common_back.setVisibility(View.INVISIBLE);
    112         back_text.setVisibility(View.INVISIBLE);
    113         left_img.setVisibility(View.VISIBLE);
    114     left_img.setImageDrawable(getResources().getDrawable(resId));
    115     }
    116 
    117     /**
    118      * 设置标题和颜色
    119      * 
    120      * @param text
    121      * @param colorId
    122      *            颜色
    123      */
    124     protected void showTitleText(String text, int colorId) {
    125         if (!TextUtils.isEmpty(text)) {
    126             title_text.setText(text);
    127         }
    128         if (colorId != 0) {
    129             title_text.setTextColor(getResources().getColor(colorId));
    130         }
    131     }
    132 
    133     /**
    134      * 只设置标题文字
    135      * 
    136      * @param text
    137      */
    138     protected void showTitleText(String text) {
    139         showTitleText(text, 0);
    140     }
    141 
    142     /**
    143      * 只设置标题颜色
    144      * 
    145      * @param colorId
    146      */
    147     protected void showTitleText(int colorId) {
    148         showTitleText("", colorId);
    149     }
    150 
    151     /**
    152      * 设置中间的背景图片,没有文字
    153      * 
    154      * @param resId
    155      */
    156     @SuppressLint("NewApi")
    157     protected void showTitleBackground(int resId) {
    158         title_text.setText("");
    159         title_text.setBackground(getResources().getDrawable(resId));
    160 
    161     }
    162 
    163     /**
    164      * 设置标题右边的内容
    165      * 
    166      * @param text
    167      *            文字内容
    168      * @param colorId
    169      *            文字颜色
    170      * @param textSize
    171      *            文字大小
    172      * @param resId
    173      *            图片id
    174      */
    175     protected void showTitleRightContent(String text, int colorId,
    176             int textSize, int resId) {
    177         if (!TextUtils.isEmpty(text)) {
    178             right_text.setVisibility(View.VISIBLE);
    179             right_text.setText(text);
    180             if (colorId != 0) {
    181                 right_text.setTextColor(getResources().getColor(colorId));
    182             }
    183             if (textSize != 0) {
    184                 right_text.setTextSize(textSize);
    185             }
    186         }
    187 
    188         if (resId != 0) {
    189             right_img.setVisibility(View.VISIBLE);
    190             right_img.setImageDrawable(getResources().getDrawable(resId));
    191         }
    192     }
    193 
    194     /**
    195      * 设置标题右边的内容
    196      * 
    197      * @param text
    198      *            文字内容
    199      * @param colorId
    200      *            文字颜色
    201      * @param textSize
    202      *            文字大小
    203      */
    204     protected void showTitleRightContent(String text, int colorId, int textSize) {
    205         showTitleRightContent(text, colorId, textSize, 0);
    206     }
    207 
    208     /**
    209      * 设置文字内容、颜色、大小
    210      * 
    211      * @param text
    212      * @param colorId
    213      */
    214     protected void showTitleRightContent(String text, int colorId) {
    215         showTitleRightContent(text, colorId, 0, 0);
    216     }
    217 
    218     /**
    219      * 只设置文字内容
    220      * 
    221      * @param text
    222      */
    223     protected void showTitleRightContent(String text) {
    224         showTitleRightContent(text, 0, 0, 0);
    225     }
    226 
    227     /**
    228      * 只设置右边的图片
    229      * 
    230      * @param resId
    231      */
    232     protected void showTitleRightContent(int resId) {
    233         showTitleRightContent(null, 0, 0, resId);
    234     }
    235 
    236 }
  • 相关阅读:
    使用TCP
    socket基本
    windows内核原理及实现-----进程线程管理------3.4节中 windows中进程句柄
    windows内核原理及实现-----进程线程管理------3.3节中 windows中进程线程的数据结构
    winDBG用法
    windows调试异常机制
    MYSQL5上运行多个实例
    Mysql双主实战
    Mysql-Proxy使用
    新的旅程
  • 原文地址:https://www.cnblogs.com/tleopard/p/5602221.html
Copyright © 2011-2022 走看看