簡體   English   中英

如何在 ActionBar 中將徽標居中?

[英]How do I center a logo in the ActionBar?

我試圖在 MainActivity ActionBar 中將徽標居中,但是當我添加它時

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.thelogo);
getSupportActionBar().setDisplayUseLogoEnabled(true);

它看起來像這樣在此處輸入圖片說明

有沒有辦法將它居中而不必將我的 RelativeLayout 更改為 LinearLayout?

由於您無法訪問視圖,因此您無法將其居中,除非有一個我認為沒有的功能,這就是為什么我總是讓我自己的工具欄使用這個:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/navigation_background"
        android:layout_gravity="center"/>
</androidx.appcompat.widget.Toolbar>

現在在您的代碼中使用:

...
setSupportActionBar(toolbar);

通過將樣式更改為:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM