【问题标题】:Android setLogo isnt placed on the center of Action BarAndroid setLogo 没有放在 Action Bar 的中心
【发布时间】:2017-07-14 07:31:34
【问题描述】:

您好,我正在使用工具栏并尝试将徽标设置到操作栏的中心。但是在中间有标题,(我认为)这就是为什么徽标从操作栏的左侧开始。然后我删除了标题以将徽标放在中心,但效果不佳。我该如何解决这个问题?

这是我用来设置工具栏的方法。

 private void setToolBar(){
       toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setBackgroundColor(getResources().getColor(colorActionBar));
        toolbar.setLogo(R.drawable.logoactionbar);

    }

这是我清单中的主题

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

这里是工具栏的声明

      <<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:background="@color/colorActionBar"
    android:fitsSystemWindows="true"
    tools:context="com.anadolusigorta.rhextranet.com.anadolusigorta.rhextranet.HomeActivity">

    <View
        android:id="@+id/statusBarBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:background="@color/colorActionBar"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorActionBar"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logoactionbar"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title" />

    </android.support.design.widget.AppBarLayout>

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

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 你已经创建了一个自定义工具栏
  • 你可能想要使用 ReleativeLayout 而不是 CoordinatorLayout

标签: android xml android-actionbar android-toolbar


【解决方案1】:

试试这个:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:src="@android:drawable/btn_star_big_off"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>

</android.support.v7.widget.Toolbar>

【讨论】:

  • 当我做这个标志没有显示。当我做这个标志没有显示。我正在使用协调器布局,我正在更新应用栏的 xml 声明
  • 在更改我的 xml 后,我接受它作为答案
【解决方案2】:

试试这个方法

<android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/your_logo"
                    android:layout_gravity="center"
                    android:id="@+id/toolbar_title" />

            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>

【讨论】:

  • 当我这样做时,没有显示这个标志。我正在使用协调器布局,我正在更新应用栏的 xml 声明
【解决方案3】:

Toolbar 只是一个 ViewGroup,您可以根据需要对其进行自定义。

试试这个:

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/color_primary"
        android:minHeight="@dimen/abc_action_bar_default_height_material">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher"/>

</android.support.v7.widget.Toolbar>

【讨论】:

  • 这似乎是正确的方法,但没有给出正确的结果。标志放在下面
猜你喜欢
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多