android title 封装

 # 自定义titlebar最近在新写一个项,界面的标题栏都是差不多的,所以采用了一些封装.结合了网上的一些资料源链接 http://www.2cto.com/kf/201608/536233.html

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_root_group"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@color/title_bg"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:id="@+id/ll_left"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center">

        <ImageView
            android:id="@+id/title_left_iv"
            android:layout_width="@dimen/title_img_size"
            android:layout_height="@dimen/title_img_size"
            android:src="@drawable/ssdk_back_arr" />

        <TextView
            android:id="@+id/title_left_tv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="返回"
            android:textSize="@dimen/title_right_left_size"
            android:visibility="gone" />
    </LinearLayout>

    <TextView
        android:id="@+id/title_name_tv"
        android:layout_width="0dp"
        android:text="爱筹"
        android:gravity="center"
        android:textSize="@dimen/title_size"
        android:layout_height="match_parent"
        android:layout_weight="6" />

    <LinearLayout
        android:id="@+id/ll_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center">

        <ImageView
            android:id="@+id/title_right_iv"
            android:layout_width="@dimen/title_img_size"
            android:layout_height="@dimen/title_img_size"
            android:src="@drawable/ssdk_logo"
            android:visibility="gone" />

        <TextView
            android:id="@+id/title_right_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/default_ptr_flip"
            android:text="下一步"
            android:textSize="@dimen/title_right_left_size" />
    </LinearLayout>
</LinearLayout>

在自己的布局中引入 

方式如下

<include layout="@layout/title">

</include>
然后写了一个 父类 activity 在里边加载这个view  创建时声明使用自己的title 然后把子类的调用的色图contentView 调用他的重载方法.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(hasTitle()){
        Window w = getWindow();
        int titleLayoutId = getCustomTitleLayoutId();
        w.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, titleLayoutId);
    }
}


@Override
public void setContentView(int layoutResID) {
    View contentView = LayoutInflater.from(this).inflate(layoutResID, null);
    setContentView(contentView);
    initTitle();
}

initTitle 中 初始化界面

/**
 * 初始化View,分三部分:
 * Left、Center、Right:可以设置图片、文字、点击事件
 */
private void initTitle() {
    titleLeftTV = (TextView) findViewById(R.id.title_left_tv);
    titleLeftIV = (ImageView) findViewById(R.id.title_left_iv);
    titleNameTV = (TextView) findViewById(R.id.title_name_tv);
    titleRightTV = (TextView) findViewById(R.id.title_right_tv);
    titleRightIV = (ImageView) findViewById(R.id.title_right_iv);
    LinearLayout ll_left = (LinearLayout) findViewById(R.id.ll_left);
    LinearLayout ll_frght= (LinearLayout) findViewById(R.id.ll_right);
    //下面三个方法根据自己的要求实现;
    setTitleStyle(titleLeftTV, titleLeftIV,titleNameTV,titleRightTV, titleRightIV);

    ll_left.setOnClickListener(this);
    ll_frght.setOnClickListener(this);
    titleNameTV.setOnClickListener(this);
} 

通过 左右view 的父布局实现点击时间 ,写了一个 setTitleStyle 让子类实现可以控制左右的样式 ,点击监听也是由子类处理

@Override
public void onClick(View view) {
    int id = view.getId();
    switch (id) {
        case R.id.ll_left:
            onTitleClick( LEFT);
            break;
        case R.id.title_name_tv:
            onTitleClick(CENTER);
            break;
        case R.id.ll_right:
            onTitleClick(RIGHT);
            break;
    }
}

public  void onTitleClick(int i ) {
  //  Toast.makeText(getBaseContext(),"sdfs",Toast.LENGTH_SHORT).show();
}
/**
 *  设置标题栏
 * @param titleLeftTV 左侧标题
 * @param titleLeftIV 左侧图片
 * @param titleNameTV 中间标题
 * @param titleRightTV 右侧标题
 * @param titleRightIV  右侧图片
 */
public void setTitleStyle(TextView titleLeftTV, ImageView titleLeftIV, TextView titleNameTV, TextView titleRightTV, ImageView titleRightIV) {
}

子类调用 

public class ProductCrowdFundingActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_crowd_funding_activity);
    }
    @Override
    public void onTitleClick(int item) {
        switch (item) {
            case BaseActivity.RIGHT:
                break;

            case BaseActivity.CENTER:
                break;
            case BaseActivity.LEFT:
                break;
    }
    }

    @Override
    public void setTitleStyle(TextView titleLeftTV, ImageView titleLeftIV, TextView titleNameTV, TextView titleRightTV, ImageView titleRightIV) {
        super.setTitleStyle(titleLeftTV, titleLeftIV, titleNameTV, titleRightTV, titleRightIV);
        titleNameTV.setText("填写项目内容2");
    }
}


所以子类activity 只要集成这个类 同时实实现 最后这两个方法 就可以控制title的样式,  其他的不需要改

这个是我自己思考的一些封装,不合理的地方请指明



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值