zoukankan      html  css  js  c++  java
  • android SDK开发 -- TitleBar封装(一)

    假设app的title 统一的都是这种左中右结构的 代码如下

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     2     style="@style/app_title_style"  
     3     android:baselineAligned="false"  
     4     android:gravity="center_vertical"  
     5     android:orientation="horizontal">  
     6     <ViewSwitcher  
     7         android:id="@+id/app_title_left_switcher"  
     8         android:layout_width="wrap_content"  
     9         android:layout_height="wrap_content">  
    10         <TextView  
    11             android:id="@+id/app_title_left_text"  
    12             android:layout_width="wrap_content"  
    13             android:layout_height="wrap_content"  
    14             android:text="返回"/>  
    15         <ImageView  
    16             android:id="@+id/app_title_left_image"  
    17             android:layout_width="wrap_content"  
    18             android:layout_height="wrap_content"/>  
    19     </ViewSwitcher>  
    20     <ViewSwitcher  
    21         android:id="@+id/app_title_middle_switcher"  
    22         android:layout_width="0dip"  
    23         android:layout_height="wrap_content"  
    24         android:layout_marginLeft="16dip"  
    25         android:layout_weight="1">  
    26         <TextView  
    27             android:id="@+id/app_title_middle_text"  
    28             android:layout_width="wrap_content"  
    29             android:layout_height="wrap_content"  
    30             android:layout_gravity="center"  
    31             android:text="title"/>  
    32         <ImageView  
    33             android:id="@+id/app_title_middle_image"  
    34             android:layout_width="wrap_content"  
    35             android:layout_height="wrap_content"  
    36             android:layout_gravity="center"/>  
    37     </ViewSwitcher>  
    38     <ViewSwitcher  
    39         android:id="@+id/app_title_right_switcher"  
    40         android:layout_width="wrap_content"  
    41         android:layout_height="wrap_content">  
    42         <TextView  
    43             android:id="@+id/app_title_right_text"  
    44             android:layout_width="wrap_content"  
    45             android:layout_height="wrap_content"  
    46             android:text="下一步"/>  
    47         <ImageView  
    48             android:id="@+id/app_title_right_image"  
    49             android:layout_width="wrap_content"  
    50             android:layout_height="wrap_content"/>  
    51     </ViewSwitcher>  
    52 </LinearLayout>  

    先来继续完善一下BaseActivity

     1 protected void onCreate(BundlesavedInstanceState){  
     2          super.onCreate(savedInstanceState);  
     3          ActivityMgr.push(this);  
     4    
     5          findViewById();  
     6 }  
     7    
     8 // 初始化app中通用的控件  
     9 protected void findViewById(){  
    10           
    11 }  
    12 //  设置标题栏  
    13 protected void setTitle(){  
    14    
    15 }  

    然后看一下BaseActivity的具体实现类TitleDemoActivity

     1 public class TitleDemoActivity extendsBaseActivity{  
     2           
     3          protectedvoid onCreate(Bundle savedInstanceState){  
     4                    super.onCreate(savedInstanceState);  
     5          }  
     6    
     7          protectedvoid findViewById(){  
     8                    setContentView(R.layout.title_demo);  
     9                    super.findViewById();  
    10                    super.setTitle();// 设置标题栏  
    11          }  
    12 }  

    TitleBar封装

    BaseActivity的设计初衷是所有的Activity的都继承该类。

    首先定义一些通用的属性、以及方法

     1 private ViewSwitcher mLeftSwitcher;  
     2 private ViewSwitcher mMiddleSwitcher;  
     3 private ViewSwitcher mRightSwitcher;  
     4 /** * 初始化View */  
     5 protected void findViewById() {  
     6   mLeftSwitcher = (ViewSwitcher) findViewById(R.id.app_title_left_switcher);  
     7   mMiddleSwitcher = (ViewSwitcher) findViewById(R.id.app_title_middle_switcher);  
     8   mRightSwitcher = (ViewSwitcher) findViewById(R.id.app_title_right_switcher);  
     9 }  
    10 protected void setTitle(String left, String middle, String right) {  
    11   ((TextView) mLeftSwitcher.getChildAt(0)).setText(left);  
    12   ((TextView) mMiddleSwitcher.getChildAt(0)).setText(middle);  
    13   ((TextView) mRightSwitcher.getChildAt(0)).setText(right);  
    14 }  

    子类调用

     1 public class TitleDemoActivity extends BaseActivity {  
     2   
     3     @Override  
     4     protected void onCreate(Bundle savedInstanceState) {  
     5         super.onCreate(savedInstanceState);  
     6   
     7     }  
     8   
     9     @Override  
    10     protected void findViewById() {  
    11         setContentView(R.layout.title_demo);  
    12         super.findViewById();  
    13   
    14         setTitle("返回主页", "这是一个Title", "下一个界面");  
    15     }  
    16   
    17 }  
  • 相关阅读:
    js数据类型转换与字面量
    CSS精灵图与字体图标
    CSS元素的显示与隐藏
    .net core依赖注入学习
    WebAPI 参数问题
    .net EF code first 注意事项
    C#面向对象
    家用电信光纤内网IP改为公网IP
    页面JS引用添加随机参数避免页面缓存
    JS处理URL中的中文
  • 原文地址:https://www.cnblogs.com/Sharley/p/5695779.html
Copyright © 2011-2022 走看看