Android中怎么自定义标题栏

发布时间:2021-06-12 18:47:48 作者:Leah
来源:亿速云 阅读:235

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>

关于Android中怎么自定义标题栏问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. Android 去掉app上面的标题栏
  2. Android 标题栏显示进度条

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:Java中instanceof关键字有什么用

下一篇:android中怎么实现一个自动轮播图效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》