android - Logo 在 Action Bar Android 中的中心位置

标签 android android-actionbar android-appcompat

我想在操作栏的中心显示 Logo 。这是我的自定义布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ActionBarWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_centerInParent="true"
        android:padding="8dp"/>

</RelativeLayout>

onCreate() 中,我将其用作:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_logo);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2A2A2A")));

问题是它在正常的 Fragments 和 Activity 中显示良好,但在与 Navigation Drawer 一起使用时会稍微向右移动。以下是图片:

  1. 错一个:

Wrong Display

  1. 右一:

Right Display

我做错了什么?谢谢。

最佳答案

然后尝试工具栏。试试这段代码

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize" >


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toolbar Title"
    android:src="@drawable/logo"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title" />


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

关于android - Logo 在 Action Bar Android 中的中心位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857438/

相关文章:

Android 翻译动画无法正常工作

android - 为什么我没有明确声明 Android 会选择 AppCompat 组件?

java - 为什么 appcompat-v7 :22. 2.0 会破坏 ActionMode 覆盖 ActionBar?

java - Android 布局编辑器中的 NotFoundException?

java - 当我在 Android 中使用绑定(bind)服务时,它是在后台运行,在它自己的线程上还是在主 UI 线程上运行?

android - 尝试在空对象引用上调用虚拟方法 'void android.widget.LinearLayout.setVisibility(int)'

java - 无法将 Appcompat 主题从浅色更改为全黑

android - 折叠操作栏,但仅在 RecyclerView 完全显示第一项时显示

android - ActionBarCompat 下拉菜单上的样式单选按钮

android - 删除图标但在 ActionBar 中有 HomeAsUp