第一行代码第四章读书笔记(2)自定义标题栏

自定义标题栏

无标题栏界面

在这里插入图片描述

创建标题栏布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:text="返回"
        android:background="@drawable/back_bg"
        android:layout_height="60dp" />


    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:text="标题"
        android:gravity="center"
        android:background="@drawable/title_bg"
        android:layout_height="60dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btn_edit"
        android:text="编辑"
        android:background="@drawable/edit_bg"
        android:layout_width="wrap_content"
        android:layout_height="60dp" />
</LinearLayout>

使用include的方式引用标题栏

<?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:id="@+id/main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</LinearLayout>

在这里插入图片描述

自定义布局的方式创建标题栏

package com.example.myfiveapp

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

class TitleLayout(context: Context,attrs: AttributeSet) : LinearLayout(context,attrs) {
    init {
        LayoutInflater.from(context).inflate(R.layout.title_bar,this)
        val btnBack = findViewById<Button>(R.id.btn_back)
        btnBack.setOnClickListener {
            val activity = context as Activity
            activity.finish()
        }
        val btnEdit = findViewById<Button>(R.id.btn_edit)
        btnEdit.setOnClickListener {
            Toast.makeText(context,"编辑", Toast.LENGTH_SHORT).show()
        }
    }
}
<?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:id="@+id/main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.myfiveapp.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值