ActionBar的title居中显示(Kotlin版)

2020-09-09  本文已影响0人  Tom_Ji

日常工作中,actionBar的title的默认显示位置基本上难以满足UI的要求,通常会要求居中显示,本文记录下actionBar居中显示的修改方法。

效果如图所示:

actionBar居中显示.gif

新建一个custom.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/app_name"
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
    />


style根据自己使用的主题来进行选择

Activity使用的布局文件main_activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/dice_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@drawable/empty_dice"
        tools:src="@drawable/dice_1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/roll_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Roll"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/dice_image" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn_show_center"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/show_center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/btn_show_default"
        app:layout_constraintBottom_toBottomOf="parent"
        />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn_show_default"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/show_default"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/btn_show_center"
        app:layout_constraintBottom_toBottomOf="parent"
         />



</androidx.constraintlayout.widget.ConstraintLayout>

dice_imageroll_button是用来显示🎲的相关内容,可以不用关注。

MainActivity.kt的代码如下:

package com.tom.actionbarcenterdemo

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.ActionBar
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.LinearLayoutCompat

class MainActivity : AppCompatActivity() {

    private lateinit var mDiceImage: AppCompatImageView
    private lateinit var mCenterBtn: AppCompatButton
    private lateinit var mDefaultBtn: AppCompatButton

    private lateinit var mCustomView: View


    @SuppressLint("InflateParams")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setHomeButtonEnabled(true)

        //获取要显示的title
        mCustomView = LayoutInflater.from(this).inflate(R.layout.custom, null)

        val actionBarLayout = LinearLayoutCompat(this)

        supportActionBar?.setCustomView(
            actionBarLayout,
            ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT
            )
        )

        val params: ActionBar.LayoutParams = actionBarLayout.layoutParams as ActionBar.LayoutParams

        params.gravity = params.gravity and Gravity.HORIZONTAL_GRAVITY_MASK.inv() or Gravity.CENTER_HORIZONTAL

        actionBarLayout.addView(mCustomView)
        supportActionBar?.setCustomView(actionBarLayout, params)

        val rollButton = findViewById<AppCompatButton>(R.id.roll_button)
        mDiceImage = findViewById(R.id.dice_image)
        mCenterBtn = findViewById(R.id.btn_show_center)
        mDefaultBtn = findViewById(R.id.btn_show_default)

        rollButton.setOnClickListener { rollDice() }
        mCenterBtn.setOnClickListener { showCenter() }
        mDefaultBtn.setOnClickListener { showDefault() }

    }

    private fun showDefault() {
        supportActionBar?.setDisplayShowCustomEnabled(false)
        supportActionBar?.setDisplayShowTitleEnabled(true)

    }

    private fun showCenter() {
        supportActionBar?.setDisplayShowCustomEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        return when (item.itemId) {
            android.R.id.home -> {
                finish()
                true
            }
            else -> super.onOptionsItemSelected(item)

        }
    }

    private fun rollDice() {
        mDiceImage.setImageResource(getRandomDiceImage())
    }

    private fun getRandomDiceImage(): Int {
        val randomInt = (1..6).random()

        return when (randomInt) {
            1 -> R.drawable.dice_1
            2 -> R.drawable.dice_2
            3 -> R.drawable.dice_3
            4 -> R.drawable.dice_4
            5 -> R.drawable.dice_5
            else -> R.drawable.dice_6
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读