Add function key to title bar

Example renderings:

How to achieve it?
1. Create a layout and reset the layout of the title block

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

    <Button
        android:id="@+id/leftButton"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp" />

    <TextView
        android:id="@+id/titleText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center" />

    <Button
        android:id="@+id/rightButton"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp" />
</RelativeLayout>

2. Edit the custom property file to set the title block content

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Topbar">
        <attr name="leftBackground" format="reference" />
        <attr name="rightBackground" format="reference" />
        <attr name="titleText" format="string" />
        <attr name="titleTextSize" format="dimension" />
        <attr name="titleTextColor" format="color" />
    </declare-styleable>
</resources>

3. Write the Topbar control, inherit the RelativeLayout, obtain the custom property and assign a value to the corresponding control.

package com.example.cactus.topbardemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Custom Topbar
 */

public class Topbar extends RelativeLayout {

    private Button leftButton, rightButton;
    private TextView titleTextView;
    private OnLeftAndRightClickListener listener;//Listen to click events

    //Set up a listener
    public void setOnLeftAndRightClickListener(OnLeftAndRightClickListener listener) {
        this.listener = listener;
    }

    //Button click interface
    public interface OnLeftAndRightClickListener {
        void OnLeftButtonClick();
        void OnRightButtonClick();
    }
    //Set the visibility of the buttons on the left
    public void setLeftButtonVisibility(boolean flag){
        if (flag)
            leftButton.setVisibility(View.VISIBLE);
        else
            leftButton.setVisibility(View.GONE);
    }

    //Set the visibility of the buttons on the right
    public void setRightButtonVisibility(boolean flag){
        if (flag)
            rightButton.setVisibility(View.VISIBLE);
        else
            rightButton.setVisibility(View.GONE);
    }
    public Topbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_topbar, this);
        leftButton = (Button) findViewById(R.id.leftButton);
        rightButton = (Button) findViewById(R.id.rightButton);
        titleTextView = (TextView) findViewById(R.id.titleText);
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnLeftButtonClick();//Click callback
                }
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnRightButtonClick();//Click callback
                }
            }
        });

        //Get custom properties and assign values
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.Topbar);
        int leftBtnBackground = typeArray.getResourceId(R.styleable.Topbar_leftBackground, 0);
        int rightBtnBackground = typeArray.getResourceId(R.styleable.Topbar_rightBackground, 0);
        String titleText = typeArray.getString(R.styleable.Topbar_titleText);
        float titleTextSize = typeArray.getDimension(R.styleable.Topbar_titleTextSize, 0);
        int titleTextColor = typeArray.getColor(R.styleable.Topbar_titleTextColor, 0x38ad5a);
        //Release resources
        typeArray.recycle();
        leftButton.setBackgroundResource(leftBtnBackground);
        rightButton.setBackgroundResource(rightBtnBackground);
        titleTextView.setText(titleText);
        titleTextView.setTextSize(titleTextSize);
        titleTextView.setTextColor(titleTextColor);
    }
}

4. Set the color change effect of left and right buttons in the title bar
Left button settings:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/b1" android:state_pressed="true"/>
    <item android:drawable="@drawable/b2" />
</selector>

Right button settings:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/r1" android:state_pressed="true"/>
    <item android:drawable="@drawable/r2" />
</selector>

5. Add Topbar to the required xml layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.cactus.topbardemo.MainActivity">

    <com.example.cactus.topbardemo.Topbar
        android:id="@+id/topbar"
        android:layout_width="368dp"
        android:layout_height="50dp"
        android:background="#000"
        app:leftBackground="@drawable/leftselecter"
        app:rightBackground="@drawable/rightselecter"
        app:titleText="My home page"
        app:titleTextColor="#FFF"
        app:titleTextSize="6sp"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

6. Define the functions implemented after clicking in the corresponding Activity

package com.example.cactus.topbardemo;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Topbar topBar = (Topbar) findViewById(R.id.topbar);
        topBar.setOnLeftAndRightClickListener(new Topbar.OnLeftAndRightClickListener() {
            @Override
            public void OnLeftButtonClick() {
// Function logic implemented by left button
                Toast.makeText(MainActivity.this, "Ha-ha", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void OnRightButtonClick() {
// Function logic realized by right button
                Toast.makeText(MainActivity.this, "RightButton", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Is that over? No, at this time, the result of your running will be as follows:

So you need to remove the original title bar, that is to do the following:
Modify the theme in mainfest.xml

android:theme="@style/Theme.AppCompat.NoActionBar"

Content reference source: link: https://www.jianshu.com/p/0763114e7738

Tags: Android xml encoding

Posted on Sun, 03 May 2020 11:47:01 -0400 by wrong_move18