首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android ActionBar的自定义视图不填充父级

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

Stack Overflow用户
提问于 2014-12-04 23:52:54
回答 4查看 14K关注 0票数 19

与问题here类似,但有一些关键的区别。最值得注意的是,直到最新的支持库发布之前,公认的答案都是有效的。

下面是自定义视图布局:

<?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。一旦我这样做了,我最终在应用程序上遇到了同样的问题,而这些应用程序以前没有出现过这个问题。

所以,我的问题是,有没有一种方法可以使用最新的支持库让自定义视图actionbar填充宽度?

编辑在做了更多的测试后,我发现将targetSdkVersion/compileSdkVersion设置为19,并且库的版本为v7:19.0.1 (或v7:19.1.0)的编译没有这个问题(有主页图标,后面的代码会处理它),确认这肯定是最新版本的问题。

EN

回答 4

Stack Overflow用户

发布于 2015-06-03 09:54: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);

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>
票数 31
EN

Stack Overflow用户

发布于 2015-07-29 14:25:37

使用Toolbar parent = (Toolbar) customNav.getParent();parent.setContentInsetsAbsolute(0,0);它可以工作

票数 7
EN

Stack Overflow用户

发布于 2016-06-13 15:17:21

在您的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);
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27298282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档