为什么我的自定义操作栏在使用 appcompat 和工具栏时没有显示 "match parent"?

Why doesn't my custom action bar view "match parent" when using appcompat and Toolbar?

我正在操作栏中设置自定义视图。它在使用 SDK 操作栏时填充操作栏的宽度,但在使用 appcompat 版本时不会填充操作栏的宽度。为什么我的线性布局在使用 appcompat 和工具栏时没有填满宽度?我该怎么办?

这是应用程序兼容前的样子:

这就是 appcompat 的样子。 (红色显示我的自定义视图的范围。):

编辑: 这是使用@Seidan 的显式布局参数提示的外观。 (在应该是白色的地方显示黑色文本):

这是我的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#800"
    >

    <FrameLayout
        android:id="@+id/actionbar_discard"
        style="?attr/actionButtonStyle"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >

        <TextView
            style="?android:actionBarTabTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableLeft="@drawable/ic_close_white_24dp"
            android:drawablePadding="8dp"
            android:gravity="center_vertical"
            android:paddingRight="20dp"
            android:text="@string/actionbar_cancel" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/actionbar_done"
        style="?attr/actionButtonStyle"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <TextView
            style="?android:actionBarTabTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableLeft="@drawable/ic_check_white_24dp"
            android:drawablePadding="8dp"
            android:gravity="center_vertical"
            android:paddingRight="20dp"
            android:text="@string/actionbar_done" />
    </FrameLayout>
</LinearLayout>

下面是我设置操作栏的方式:

ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayOptions(
        ActionBar.DISPLAY_SHOW_CUSTOM,
        ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
ab.setCustomView(R.layout.actionbar_discard_done);

appcompat-v7:22.0.0 和新的 appcompat-v7:22.1.0 都存在这个问题。

感谢@Seidan 的评论。它使我找到了解决方案。样式问题是由于充气器使用了错误的主题,当我给它我的 Activity 的 this.

总结一下,而不是问题中我代码中的这一行...

ab.setCustomView(R.layout.actionbar_discard_done);

...我有...

View v = LayoutInflater
        .from(ab.getThemedContext())
        .inflate(R.layout.actionbar_discard_done, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
        ActionBar.LayoutParams.MATCH_PARENT, 
        ActionBar.LayoutParams.MATCH_PARENT);
ab.setCustomView(v, params);

这让我...

为什么我们必须在布局和代码中指定 match_parent 是个谜,但我可以接受。

在onCreate

RelativeLayout relativeLayout_custombar = (RelativeLayout)findViewById(R.id.relativeLayout_custombar);

Display display = getWindowManager().getDefaultDisplay();
int x_size = display.getWidth();


LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.your_customview, null);


getSupportActionBar().setCustomView(mCustomView);
getSupportActionBar().setDisplayShowCustomEnabled(true);


Toolbar parent = (Toolbar) mCustomView.getParent();
parent.setContentInsetsAbsolute(0, 0);


parent.findViewById(R.id.relativeLayout_custombar).getLayoutParams().width = x_size;