简体   繁体   中英

Android 4.4 Kitkat custom view actionbar not filling the whole width

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);

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

@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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM