Android 4.4 Kitkat custom view actionbar not filling the whole width

♀尐吖头ヾ 提交于 2019-12-03 07:07:59

问题


I am trying to have a Simple actionbar with a custom view, but I get the following result:

For demonstration I created a simple xml with a yellow background color which is supposed to take the whole width.

Here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow"
    android:orientation="vertical" >

    <TextView
        android:visibility="gone"
        android:id="@+id/action_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/action_save"
        android:gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/white"
        android:text="@string/save" />

    <ImageView
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_actionbar_logo" />

</RelativeLayout>

And I use the following theme for my Application:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>

This is the actionbar initialization code:

    actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(R.layout.actionbar);

回答1:


The solution is adding :

actionBar.setDisplayShowTitleEnabled(false);

The code should look something like:

actionBar = getSupportActionBar(); 
actionBar.setDisplayShowHomeEnabled(false); 
actionBar.setDisplayShowCustomEnabled(true); 
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.actionbar);//set the custom view



回答2:


@NemesisDoom: Please make sure that you use ActionBar from the support.v7 library rather that one from the Android API (this second one will not work certainly). If still doesn't work, add the further changes:

styles.xml (your action bar style section):

 <item name="android:homeAsUpIndicator">@drawable/_pixel</item>
    <item name="homeAsUpIndicator">@drawable/_pixel</item>

_pixel is 1x1.png image, transparent.

your activity:

actionBar.setDisplayHomeAsUpEnabled(true);


来源:https://stackoverflow.com/questions/20172256/android-4-4-kitkat-custom-view-actionbar-not-filling-the-whole-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!