自定义标题栏,让自己以后不那么累

本文介绍了一种在Android应用中创建自定义标题栏的方法。通过继承LinearLayout并利用LayoutInflater加载自定义布局,实现了包含返回按钮、前进按钮及标题文本的标题栏组件。该组件支持颜色定制,并集成了点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • MainActivity:
    • 只需要在onCreate()的中添加:Custom_Title  title=new Custom_Title(this);
  • MainActivity所对应的xml文件:
    • <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout 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"
          tools:context="com.example.mac.customtitle.MainActivity">

          <com.example.mac.customtitle.Custom_Title
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
      </RelativeLayout>
    •     <com.example.mac.customtitle.Custom_Title>代表的是自定义标题的代码文件
  • 自定义标题栏代码:
    • package com.example.mac.customtitle;

      import android.content.Context;
      import android.graphics.Color;
      import android.provider.CalendarContract;
      import android.support.annotation.ColorInt;
      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;

      /**
       * Created by mac on 16/8/11.
       */
      public class Custom_Title extends LinearLayout implements View.OnClickListener{
          public Custom_Title(Context contextAttributeSet attrs) {
              super(contextattrs);
              //加载布局
              View view=LayoutInflater.from(context).inflate(R.layout.custom_title,this);
              //设置布局控件
              TextView textView=(TextView)view. findViewById(R.id.tv_title);
              Button button_back= (Button)view. findViewById(R.id.bt_back);
              Button button_next= (Button)view. findViewById(R.id.bt_next);
              //给控件设置属性:
              view.setBackgroundColor(Color.GRAY);
              button_back.setBackgroundColor(Color.BLUE);
              button_next.setBackgroundColor(Color.CYAN);
              button_back.setOnClickListener(this);
              button_next.setOnClickListener(this);
              textView.setText("自定义标题");

          }

          public Custom_Title(Context context) {
              super(context);
          }

          public Custom_Title(Context contextAttributeSet attrs, int defStyleAttr) {
              super(contextattrsdefStyleAttr);
          }

          @Override
          public void onClick(View view) {
              switch (view.getId()){
                  case R.id.bt_back:
                      Toast.makeText(getContext(),"Im's back",Toast.LENGTH_LONG).show();
                      break;
                  case R.id.bt_next:
                      Toast.makeText(getContext(),"Im's next",Toast.LENGTH_LONG).show();
                      break;
              }
          }
      }
  • XML文件:就是给Title布局:
    • <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
          <Button
              android:id="@+id/bt_back"
              android:text="back"
              android:layout_width="wrap_content"
              android:layout_height="match_parent" />
          <TextView
              android:id="@+id/tv_title"
              android:layout_gravity="center"
              android:gravity="center"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:text="自定义标题"
              android:layout_height="match_parent" />
          <Button
              android:id="@+id/bt_next"
              android:text="next"
              android:layout_width="wrap_content"
              android:layout_height="match_parent" />

      </LinearLayout>
    • OK,自定义的标题栏就算完成了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值