getSupportActionBar().setCustomView(view)不会填充整个操作栏

ale*_*709 9 android android-styles android-actionbar-compat android-actionbaractivity

我正在尝试设置我的应用程序的自定义视图,我制作的布局没有填满整个操作栏.我尝试了很多样式设置,但没有一个适合我.

这是我的活动代码

View view = getLayoutInflater().inflate(R.layout.actionbar_customized_home, null);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(view);
Run Code Online (Sandbox Code Playgroud)

这里的布局设置为视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/actionbar_color"
    >
![enter image description here][1]

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_centerInParent="true"
        android:scaleType="fitCenter"
        android:src="@drawable/logo"/>



</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

甚至像导航器这样的菜单项的背景如何将我的自定义视图作为背景颜色?

我非常感谢你的帮助

clo*_*ith 21

这就是你需要添加的全部内容 onCreate()

getSupportActionBar().setCustomView(new CustomView(this));
getSupportActionBar().setDisplayShowCustomEnabled(true);
Run Code Online (Sandbox Code Playgroud)


JLO*_*ONG 8

将主容器设置为:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_horizontal"
        android:background="@color/actionbar_color"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_centerInParent="true"
            android:scaleType="fitCenter"
            android:src="@drawable/logo"/>

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

重要的android:layout_gravity="fill_horizontal是应该解决你的问题.

编辑:

如果这不起作用尝试这样做:

View view = getLayoutInflater().inflate(R.layout.actionbar_customized_home, null);
LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
getSupportActionBar().setCustomView(view, layout); 
Run Code Online (Sandbox Code Playgroud)