18

here类似,但有一些关键区别。最值得注意的是接受的答案确实工作,直到最新的支持库发布。Android ActionBar的自定义视图不填充父项

这里的自定义视图布局:

<?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); 

您使用自定义布局不填充整个结束宽度: enter image description here

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

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)的图书馆没有这个问题(拥有主图标,后面的代码会照顾它),从而确认这对于最新版本肯定是一个问题。

+0

查看关于重复问题的答案http://stackoverflow.com/a/26454325/624109 – Muzikant 2014-12-29 10:52:27

回答

26
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 Use Toolbar 


     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 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> 
+1

你能解释一下这是如何特别解决OP问题的最新版本的问题吗? – 2015-06-03 02:12:57

+0

@tuder但我得到NPE当我尝试启动我的应用程序的第一次和errro是在这一行parent.setContentInsetsAbsolute(0,0);这表明父母为空 – Erum 2015-08-18 12:44:38

+0

@Erum检查你的布局的id或你的Actionbar的版本,我使用android.support.v7.appcompat – tuder 2015-08-19 06:15:02

6

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

5

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

在你的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"> 

将装配到视图的全部宽度。