自定义标题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niu_yue_jiao/article/details/79949795

package example.com.titles;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;

public abstract class BaseActivity extends AppCompatActivity {

    private MyRelativeLayout parent_title;
    private FrameLayout child_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        //初始化控件的方法
        initParentView();

        //得到xml里面的id
        int id = this.setXmlContentView();
        //得到MyRelativeLayout的布局
        View view = View.inflate(BaseActivity.this, id, null);
        //将布局添加到baseActivity里面
        child_view.addView(view);

        //供继承的方法
        initView();
        initData();
        setXmlContentView();
    }

    private void initParentView() {
        parent_title = findViewById(R.id.parent_title);
        child_view = findViewById(R.id.child_view);

        parent_title.getBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseActivity.this.finish();
            }
        });

    }

    public MyRelativeLayout getTitleBar() {

        return parent_title;
    }

    protected abstract void initView();

    protected abstract void initData();

    protected abstract int setXmlContentView();

}
package example.com.titles;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

    private ImageButton mainId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        MyRelativeLayout titleBar = getTitleBar();
        TextView title = titleBar.getTitle();
        title.setText("主页");

        //找控件
        mainId = findViewById(R.id.mainId);
        //跳转
        mainId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ShopActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int setXmlContentView() {
        return R.layout.activity_main;
    }
}
package example.com.titles;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * author:Created by niuyuejiao on 2018/4/13.
 */

public class MyRelativeLayout extends RelativeLayout {

    private TextView textView;
    private ImageView backImg;

    public MyRelativeLayout(Context context) {
        this(context, null);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        //创建标题
        RelativeLayout.LayoutParams pl_tv = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        pl_tv.addRule(RelativeLayout.CENTER_IN_PARENT);//居中
        textView = new TextView(context);
        textView.setText("标题");
        textView.setId(R.id.titleBar_id);//设置id
        textView.setTextSize(30);
        textView.setTextColor(Color.WHITE);
        addView(textView, pl_tv);//添加到视图

        //创建返回按钮
        RelativeLayout.LayoutParams pl_img = new RelativeLayout.LayoutParams(100, 100);
        pl_img.addRule(RelativeLayout.ALIGN_LEFT);//居左
        pl_img.leftMargin = 20;
        backImg = new ImageView(context);
        backImg.setImageResource(R.mipmap.back_01);
        addView(backImg, pl_img);//添加到视图中
    }

    //返回TextView供调用
    public TextView getTitle() {

        return textView;
    }

    //backImg
    public ImageView getBack() {

        return backImg;
    }
}
package example.com.titles;

import android.os.Bundle;
import android.widget.TextView;

public class ShopActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        MyRelativeLayout titleBar = getTitleBar();
        TextView title = titleBar.getTitle();
        title.setText("分类");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int setXmlContentView() {
        return R.layout.activity_shop;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context="example.com.titles.BaseActivity">

    <example.com.titles.MyRelativeLayout
        android:id="@+id/parent_title"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorAccent">

    </example.com.titles.MyRelativeLayout>

    <FrameLayout
        android:id="@+id/child_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

阅读更多

没有更多推荐了,返回首页