Android之自定义标题栏(组合控件)

简单的自定义标题栏

android自定义控件向来都是开发者最头疼的,但是我们要有那种迎难而上的精神。

MainActivity
package com.example.customview;

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

/*
    android自定义标题组合控件
    步骤:
    1.首先写出需要功能的布局xml,分析布局的父控件是谁?
    例如水平布局 父控件应该是linearlayout较为合适
    2.创建自定义控件类并继承xml父控件
    3.在构造方法中使用layoutInflat动态加载布局
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //去除自带标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }

}
TitleLayout.class
package com.example.customview.custom;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.customview.R;

/**
 * 自定义标题栏 并赋有点击事件
 */
public class TitleLayout extends LinearLayout implements View.OnClickListener {
    private Button btback, btopen;
    private TextView tvtitle;

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        //动态加载标题栏布局
        LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
        initView();
    }

    private void initView() {//初始化控件
        btback = (Button) findViewById(R.id.btback);
        btback.setOnClickListener(this);
        btopen = (Button) findViewById(R.id.btopen);
        btopen.setOnClickListener(this);
        tvtitle = (TextView) findViewById(R.id.tvtitle);
        tvtitle.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {//监听点击事件
        switch (view.getId()) {
            case R.id.btback:
                ((Activity) getContext()).finish();
                Toast.makeText(getContext(), "销毁当前Activity", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btopen:
                Toast.makeText(getContext(), "展开", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tvtitle:
                Toast.makeText(getContext(), "标题", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.customview.MainActivity">

    <include layout="@layout/custom_layout" />

    <com.example.customview.custom.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.customview.MainActivity">

    <include layout="@layout/custom_layout" />

    <com.example.customview.custom.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

粘贴以上代码就可以运行了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值