Android的动作条的自定义视图不填充父 [英] Android ActionBar's custom view not filling parent

查看:154
本文介绍了Android的动作条的自定义视图不填充父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题<一href="http://stackoverflow.com/questions/20172256/android-4-4-kitkat-custom-view-actionbar-not-filling-the-whole-width">here,但有一些关键的差别。最值得注意的是在于接受答案的没有的工作,直到最新的支持库版本。

下面是自定义视图布局:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:方向=垂直
              机器人:layout_width =match_parent
              机器人:layout_height =match_parent
              机器人:后台=#FFDD0000&GT;

&LT; / LinearLayout中&GT;
 

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

 动作条动作条= getSupportActionBar();
  actionBar.setDisplayShowCustomEnabled(真正的);
  actionBar.setCustomView(R.layout.actionbar);
 

您结束了一个自定义布局不填满整个宽度:

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

 动作条动作条= getSupportActionBar();
  actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  actionBar.setCustomView(R.layout.actionbar);
  actionBar.setDisplayHomeAsUpEnabled(假);
  actionBar.setHomeButtonEnabled(假);
 

本的没有的工作,直到我更新了支持库V21。有一次,我做到了,我结束了在应用程序相同的问题,即previously还没有这个问题。

所以,我的问题是,有没有一种方法,使自定义视图动作条采用了最新的支持库填补了宽?

修改做一些更多的测试后,我发现,targetSdkVersion / compileSdkVersion编译设置为19,并具有V7:19.0.1(或V7:19.1.0)对于图书馆没有这个问题(有家庭的图标,这是照顾与后来的code),证实了这绝对是一个问题的最新版本。

解决方案

 动作条动作条= getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(假);
    actionBar.setDisplayShowCustomEnabled(真正的);
    actionBar.setDisplayShowTitleEnabled(假);
    查看查看= getLayoutInflater()。膨胀(R.layout.actionbar_title_back,
            空值);
    的LayoutParams的LayoutParams =新的LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
    actionBar.setCustomView(视图的LayoutParams);
    工具栏父=(工具栏)view.getParent();
    parent.setContentInsetsAbsolute(0,0);
 

Similar to the question here, but has some key differences. Most notably being that the accepted answer did work until the latest support library releases.

Here's the custom view layout:

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

Now, when you try to set just the custom view:

  ActionBar actionBar = getSupportActionBar();
  actionBar.setDisplayShowCustomEnabled(true);
  actionBar.setCustomView(R.layout.actionbar);

You end up with a custom layout that doesn't fill the entire width:

To make this more interested, I have a similar setup in a different app:

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

This did work, until I updated the support libraries to v21. Once I did, I ended up with the same issue on apps that previously haven't had that issue.

So, my question is, is there a way to make a custom view actionbar fill the width using the latest support library?

Edit After doing some more tests, I found that compiling with targetSdkVersion/compileSdkVersion set to 19 and having v7:19.0.1 (or v7:19.1.0) for the library doesn't have this issue (has the home icon, which is taken care of with the later code), confirming that this is definitely an issue with the latest release.

解决方案

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

这篇关于Android的动作条的自定义视图不填充父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆