Java 如何在Android工具栏中居中放置按钮

Java 如何在Android工具栏中居中放置按钮,java,android,android-toolbar,Java,Android,Android Toolbar,我是android应用程序开发新手。我正在尝试创建一个工具栏,它有3个按钮,左中和中。我的左边是正确的按钮被正确地定位,但是我的中间按钮显示在页面的中间而不是工具条的中间。想知道是否有人能帮上忙。谢谢 还想知道是否有人可以帮助处理工具栏上方的空白 主要活动: <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:and

我是android应用程序开发新手。我正在尝试创建一个工具栏,它有3个按钮,左中和中。我的左边是正确的按钮被正确地定位,但是我的中间按钮显示在页面的中间而不是工具条的中间。想知道是否有人能帮上忙。谢谢

还想知道是否有人可以帮助处理工具栏上方的空白

主要活动:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        >


    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbarlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/menu_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Menu" />

        <Button
            android:id="@+id/add_new_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_centerInParent="true"
            android:text="Add New" />

        <Button
            android:id="@+id/home_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Home" />

    </RelativeLayout>


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#141619"
        android:fitsSystemWindows="true"
        app:itemIconTint="#141619"
        app:itemTextColor="#FFFFFF"
        app:menu="@menu/activity_main_drawer" >

    </com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>



我不知道你想做的是不是个好主意。 从设计角度来看,工具栏中的空间有限,应该只定位与上下文最相关的操作,所有其他操作都将隐藏在经典的“三点”菜单中

你可以看看我的意思


尽管如此,为了实现您想要做的事情,我将使用约束布局而不是相对布局,使您的生活更加轻松,并且是最新的;)

首先,创建
title\u menu.xml
Layout。确保
android:layout\u height=“wrap\u content”
android:gravity=“center”


我已经修复了所有布局的问题

  • 对于您的活动_main.xml,请复制并粘贴到布局下方

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    
            <include
                android:id="@+id/app_bar_main"
                layout="@layout/app_bar_main" />
    
    
        </RelativeLayout>
    
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#141619"
            android:fitsSystemWindows="true"
            app:itemIconTint="#141619"
            app:itemTextColor="#FFFFFF">
    
        </com.google.android.material.navigation.NavigationView>
    </androidx.drawerlayout.widget.DrawerLayout>
    

    如果你有任何问题,请告诉我。我会尽力帮你的。

    如果你想的话,它是android:layout\u centerHorizontal=“true”
  • 而不是在父级中居中。但是,这并不是构造具有左-中-右按钮的工具的一种假定方法。你所需要的只是创建一个包含3个按钮的布局。这很有效,谢谢。你所说的自己的布局是指一个新的布局文件,然后只包含在活动主目录中,对吗?而且我的顶部还有空白。想知道你是否可以建议删除它?
    <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#141619"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    
        />
    
    <?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="wrap_content"
        android:gravity="center">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Button"/>
    </LinearLayout>
    
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.title_menu);
    
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    
            <include
                android:id="@+id/app_bar_main"
                layout="@layout/app_bar_main" />
    
    
        </RelativeLayout>
    
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#141619"
            android:fitsSystemWindows="true"
            app:itemIconTint="#141619"
            app:itemTextColor="#FFFFFF">
    
        </com.google.android.material.navigation.NavigationView>
    </androidx.drawerlayout.widget.DrawerLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#282941"
        android:fitsSystemWindows="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:minHeight="?attr/actionBarSize">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingEnd="16dp">
    
            <Button
                android:id="@+id/menu_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Menu" />
    
            <Button
                android:id="@+id/add_new_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_centerInParent="true"
                android:text="Add New" />
    
            <Button
                android:id="@+id/home_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Home" />
        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>
    
    Toolbar toolbar = findViewById(R.id.account_toolbar);
    setSupportActionBar(toolbar);
    Button btnEdit = toolbar.findViewById(R.id.menu_button);