【问题标题】:how to place logo in the center of actionbar toolbar in android?如何在android的动作栏工具栏的中心放置徽标?
【发布时间】:2018-07-16 01:51:33
【问题描述】:

我想在操作栏的中心显示徽标。这是我的布局代码:下面的代码应该可以工作,但为什么没有?请帮帮我

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

</LinearLayout>

并且在 onCreate()

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.custom_logo);

【问题讨论】:

  • 我试过这个链接,但它对我不起作用
  • 我的标志没有居中
  • 添加您获得和想要的屏幕截图
  • 我只想将我的徽标放在操作栏的中心。我尝试了所有代码,但我不明白为什么不在工具栏的中心

标签: android android-actionbar position toolbar


【解决方案1】:

您的工具栏就像一个可以容纳其他视图的 ViewGroup。因此,更简单的方法是将 ImageView 放置在工具栏中。例如:-

    <android.support.v7.widget.Toolbar
        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="@color/material_blue"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:theme="@style/ToolbarCustomIconColor" >
             <RelativeLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_content" >
                 <ImageView
                     android:id="@+id/imgLogo"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_centerInParent="true"
                     android:src="@drawable/your_icon" />
         </RelativeLayout>
    </android.support.v7.widget.Toolbar>

您可以像这样创建一个自定义工具栏,并随心所欲地对其进行操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多