Android的动作条的自定义视图不填充父自定义、视图、动作、Android

2023-09-07 08:37:07 作者:看不透忘不了

类似的问题here,但有一些关键的差别。最值得注意的是在于接受答案的没有的工作,直到最新的支持库版本。

下面是自定义视图布局:

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

< / LinearLayout中>
 

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

 动作条动作条= getSupportActionBar();
  actionBar.setDisplayShowCustomEnabled(真正的);
  actionBar.setCustomView(R.layout.actionbar);
 
android开发,UI布局问题,eclipse中不显示XML的预览视图

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

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

 动作条动作条= 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);

 
精彩推荐
图片推荐