(Android Studio) Custom Action Bar Layout isnt filling whole action bar

只愿长相守 提交于 2020-01-06 02:49:50

问题


before setContentView()

LayoutInflater inflater = (LayoutInflater) getSupportActionBar() .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.ab, null);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM,  ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView,    new android.support.v7.app.ActionBar.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,  ViewGroup.LayoutParams.MATCH_PARENT));

in ab.xml

in My Application

int styles.xml

<!-- Base application theme. -->

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

</style>
<style name="ATheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/AppTheme.ActionBar.a</item>
    <item name="actionBarStyle">@style/AppTheme.ActionBar.a</item>>
    <item name="android:icon">@android:color/transparent</item>
</style>
<style name="AppTheme.ActionBar.a" parent="Widget.AppCompat.Light.ActionBar">
    <item name="android:height">50dp</item>
    <item name="android:background">#d1d1d1</item>
</style>

in my Application, isn't filling action bar completely


回答1:


The answer is Change your style like this

<resources>

    <!-- the theme applied to the application or activity -->
    <style name="AppTheme"
        parent="Theme.AppCompat.Light.DarkActionBar">

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="Widget.AppCompat.Toolbar">

        <!-- Support library compatibility -->
        <item name="contentInsetStart">0dp</item>
        <!--<item name="background">@android:color/transparent</item>-->
    </style>

</resources>

And your ActionBar.xml Layout like this: Note: if you changed 'android:layout_height="?attr/actionBarSize"' to 'android:layout_height="match_parent"' it will not fill whole action bar width and if you changed LinearLayout to RelativeLayout with 'android:layout_height="match_parent"' Action bar will fills the entire screen

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:gravity="center"
    android:background="#0000FF"
    android:layout_height="?attr/actionBarSize">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:text="Aplication"
        />

</LinearLayout>

Finally :

setContentView(R.layout.activity_main);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.ab);

        View customActionBarView = actionBar.getCustomView();

Hope this help.



来源:https://stackoverflow.com/questions/29478494/android-studio-custom-action-bar-layout-isnt-filling-whole-action-bar

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