同福

顶部导航栏布局(含后退按钮和功能按钮)

介绍

介绍

说到 app 程序一定会有的就是顶部导航栏和底部导航栏

今天我们来学习顶部导航栏的布局设计

教程

要的

  • 使用约束布局 ConstraintLayout 实现左中右布局

  • 左边使用“靠左”“靠上”布局方式

  • 右边使用“靠右”“考上”布局方式

  • 中间使用“左靠后退按钮”“右靠功能按钮”布局方式

实例

<?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"
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="#eee">

        <ImageView
            android:id="@+id/topbar_btn_back"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:scaleType="center"
            android:src="@drawable/icon_back" />

        <TextView
            android:id="@+id/topbar_title"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="@+id/topbar_btn_back"
            app:layout_constraintRight_toRightOf="@+id/topbar_btn_fun"
            android:layout_marginTop="7dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:singleLine="true"
            android:textAlignment="center"
            android:text=""
            android:textSize="25dp"
            android:textColor="@color/colorPrimary" />

        <ImageView
            android:id="@+id/topbar_btn_fun"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:scaleType="center"
            android:src="@drawable/icon_fun" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

使用

通过 include 方式在 Activity 里引用