Android ActionBar的自定义视图未填充父级

Sha*_*zad 19 android android-support-library android-actionbar-compat android-5.0-lollipop

类似于这里的问题,但有一些关键的区别.最值得注意的是,在最新的支持库发布之前,已接受的答案确实有效.

这是自定义视图布局:

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

现在,当您尝试仅设置自定义视图时:

  ActionBar actionBar = getSupportActionBar();
  actionBar.setDisplayShowCustomEnabled(true);
  actionBar.setCustomView(R.layout.actionbar);
Run Code Online (Sandbox Code Playgroud)

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

为了使这个更感兴趣,我在不同的应用程序中有类似的设置:

  ActionBar actionBar = getSupportActionBar();
  actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  actionBar.setCustomView(R.layout.actionbar);
  actionBar.setDisplayHomeAsUpEnabled(false);
  actionBar.setHomeButtonEnabled(false);
Run Code Online (Sandbox Code Playgroud)

确实有效,直到我将支持库更新到v21.一旦我做了,我最终在以前没有遇到过这个问题的应用程序上遇到了同样的问题.

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

编辑在做了一些更多的测试之后,我发现用targetSdkVersion/compileSdkVersion设置为19进行编译并且为库提供v7:19.0.1(或v7:19.1.0)没有这个问题(有主页图标,这是用后面的代码处理),确认这绝对是最新版本的问题.

小智 29

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

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

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

  • 您能否解释这如何具体解决 OP 询问的最新版本的问题? (2认同)

jes*_*aul 7

使用工具栏parent =(工具栏)customNav.getParent(); parent.setContentInsetsAbsolute(0,0); 它会工作


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


归档时间:

查看次数:

13075 次

最近记录:

7 年,4 月 前