(Android Studio)自定义操作栏布局不填充整个操作栏

Tah*_*kem 1 android fill android-actionbar

在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));
Run Code Online (Sandbox Code Playgroud)

在ab.xml中 此搜索

在我的应用程序中

图像2

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>
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,没有完全填充操作栏

Ibr*_*uki 5

答案是改变你的风格

<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>
Run Code Online (Sandbox Code Playgroud)

你的ActionBar.xml布局是这样的: 注意:如果你将'android:layout_height ="?attr/actionBarSize"'更改为'android:layout_height ="match_parent"'它将不会填充整个操作栏宽度,如果你将LinearLayout更改为RelativeLayout与'android:layout_height ="match_parent"'操作栏将填满整个屏幕

<?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>
Run Code Online (Sandbox Code Playgroud)

最后:

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();
Run Code Online (Sandbox Code Playgroud)

希望这有帮助.