Android ActionBar的自定义视图未填满父视图

19
与此问题类似这里,但有一些关键的差别。最显著的是已接受的答案确实有效,直到最新的支持库发布为止。
下面是自定义视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#FFDD0000">

</LinearLayout>
现在,当您尝试仅设置自定义视图时:
  ActionBar actionBar = getSupportActionBar();
  actionBar.setDisplayShowCustomEnabled(true);
  actionBar.setCustomView(R.layout.actionbar);

您最终会得到一个自定义布局,它不会填满整个宽度: 在此输入图片描述

为了使这更有趣,我在另一个应用程序中设置了类似的布局:

  ActionBar actionBar = getSupportActionBar();
  actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  actionBar.setCustomView(R.layout.actionbar);
  actionBar.setDisplayHomeAsUpEnabled(false);
  actionBar.setHomeButtonEnabled(false);

在我更新支持库到v21之前,这确实有效。但一旦我更新,之前没有此问题的应用程序也遇到了相同的问题。

所以,我的问题是,有没有办法使用最新的支持库使自定义视图操作栏填充宽度?

编辑 经过更多测试,我发现将targetSdkVersion/compileSdkVersion编译设置为19并在库中使用v7:19.0.1(或v7:19.1.0)不会出现此问题(具有后来代码处理的主页图标),证实这绝对是最新版本的问题。


请参考重复问题的答案:https://dev59.com/rV8d5IYBdhLWcg3wzEv4#26454325 - Muzikant
4个回答

31

1

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
View view = getLayoutInflater().inflate(R.layout.actionbar_title_back,
            null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
actionBar.setCustomView(view, layoutParams);
Toolbar parent = (Toolbar) view.getParent();
parent.setContentInsetsAbsolute(0, 0);

2 使用工具栏

toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

XML

<?xml version="1.0" encoding="utf-8"?>       
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:popupBackground="@color/red"
        app:popupTheme="@style/PopupTheme"
        app:theme="@style/ToolbarTheme" >

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Toolbar Title"
            android:textColor="@color/white"
            android:textSize="17.0sp" />
    </android.support.v7.widget.Toolbar>
</LinearLayout>

3
你能解释一下这个具体是如何解决 OP 所问的最新版本问题的吗? - Amos M. Carpenter
@tuder,当我第一次尝试启动我的应用程序时,我遇到了NPE错误,并且错误出现在这行代码上parent.setContentInsetsAbsolute(0, 0); 这表明parent为空。 - Erum
@Erum,请检查您的布局ID或ActionBar版本,我使用android.support.v7.appcompat。 - tuder
@tuder,非常感谢。它有效了。 - Sk Saad Al Mahmud
1
谢谢。我尝试了选项1,它起作用了。注意:这里是我需要添加的导入语句:import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.LayoutParams; import android.support.v7.widget.Toolbar; - stevehs17

7

使用以下代码:

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

它可以正常工作。


6
在您的onCreate方法中添加如下内容。
 ActionBar action = getSupportActionBar();
        action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

 Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
        toolbar.setContentInsetsAbsolute(0, 0);
        toolbar.getContentInsetEnd();
        toolbar.setPadding(0, 0, 0, 0);

3

在你的XML代码中添加以下属性:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/re_app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:contentInsetEnd="0dp"
    android:contentInsetLeft="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp">

这将适应视图的所有宽度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接