【问题标题】:Why doesn't my custom action bar view "match parent" when using appcompat and Toolbar?使用 appcompat 和 Toolbar 时,为什么我的自定义操作栏视图“不匹配父级”?
【发布时间】:2015-07-09 05:06:44
【问题描述】:

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

这是 pre-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 都存在此问题。

【问题讨论】:

  • 你检查过这个答案stackoverflow.com/a/16652485/1880784吗?
  • 嗯。这样可行!但现在我的文字样式错误。 (我现在必须扩充我的自定义视图,而不仅仅是传递 ID。我猜某些上下文或主题设置不正确。)

标签: android android-appcompat material-design


【解决方案1】:

感谢@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 是个谜,但我可以接受。

【讨论】:

  • 谢谢,谢谢,谢谢。刚刚在这个问题上花了几个小时,然后找到了你的问题:)
  • 相当简单优雅的解决方案!谢谢。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多