Android Lollipop、AppCompat ActionBar 自定义视图不占用整个屏幕宽度 [英] Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width

查看:25
本文介绍了Android Lollipop、AppCompat ActionBar 自定义视图不占用整个屏幕宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我刚刚将我的代码库更新为 Lollipop,但我在操作栏上遇到了问题.我正在使用 AppCompat 和 ActionBarActivity,并扩展自定义视图.好像自定义view不再占整个屏幕宽度了,左边留了一条细条

So, I just updated my codebase to Lollipop, and I'm having issues with the Action Bar. I'm using AppCompat and ActionBarActivity, and inflating a custom view. It seems that the custom view no longer takes up the whole width of the screen, leaving a thin strip on the left

以前的样子

现在的样子

这是我用来设置操作栏的代码.有人有什么想法吗?

This is the code I'm using to set up the Action Bar. Anyone have any ideas?

final ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setCustomView(R.layout.action_bar_content_search_custom_view);
    actionBar.setBackgroundDrawable(null);
    // actionBar.setStackedBackgroundDrawable(null);
    TextView title = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title);
    title.setText(R.string.youtube);
    ImageView back = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_back);
    back.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

编辑

取出自定义视图并更改背景现在占据了整个宽度.那么问题来了,如何让一个CustomView占据ActionBar的整个宽度?

Taking out the custom view and changing the background now takes up the whole width. So the issue is, how can we make a CustomView take up the whole width of the ActionBar?

推荐答案

看起来这是由于最近 appcompat-v7 更新中对 ActionBar 的最新更改造成的.处理操作栏的方式似乎发生了重大变化.

Looks like this is caused by the latest changes to the ActionBar in the recent appcompat-v7 update. It seems like that there are significant changes to how you should handle action bars.

我遇到了同样的问题,在阅读了 ActionBar 之后文档,尤其是以下引述我找到了解决方案.

I faced the same issue and after reading the ActionBar documentation, and especially the following quote I found a solution.

从 Android L(API 级别 21)开始,操作栏可以由应用程序布局中的任何工具栏小部件表示.应用程序可以向 Activity 发出信号,哪个 Toolbar 应该被视为 Activity 的操作栏.使用此功能的活动应使用提供的 .NoActionBar 主题之一,将 windowActionBar 属性设置为 false 或不请求窗口功能.

Beginning with Android L (API level 21), the action bar may be represented by any Toolbar widget within the application layout. The application may signal to the Activity which Toolbar should be treated as the Activity's action bar. Activities that use this feature should use one of the supplied .NoActionBar themes, set the windowActionBar attribute to false or otherwise not request the window feature.

在我看来,AppCompat 主题发生了变化,一方面似乎破坏了一些东西,但另一方面提供了更多的灵活性.我建议执行以下步骤:

The way I see it, the AppCompat themes were changed and on one hand seemed to break a few things but provide much more flexibility on the other. I recommend following these steps:

  1. 在您的活动中使用 .NoActionBar 样式,如上述引用所述
  2. android.support.v7.widget.Toolbar 添加到您的活动布局
  3. 设置 app:contentInsetStart="0dp" 属性.这是您在问题中描述的边距的主要问题
  1. Use .NoActionBar style in your activity as described in the above quote
  2. Add a android.support.v7.widget.Toolbar to your Activity layout
  3. Set the app:contentInsetStart="0dp" attribute. This is the main issue with the margin that you describe in your question

<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/actionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp" >
</android.support.v7.widget.Toolbar>

通常建议您在单独的布局文件中执行此操作,并在您的活动布局中使用 include,这样如果在多个活动中使用,您只需在一个地方自定义工具栏

It's usually recommended that you do that in a separate layout file and use include in your activity layout so you will only need to customize the Toolbar in one place if used in multiple activities

<include layout="@layout/view_action_bar" />

  1. 在您的 Activity onCreate 中使用 findViewByIdsetSupportActionBar 向 Activity 发出信号,哪个 Toolbar 应该被视为 Activity 的操作栏
  1. Use findViewById and setSupportActionBar in your Activity onCreate to signal to the Activity which Toolbar should be treated as the Activity's action bar

Toolbar actionBar = (Toolbar) findViewById(R.id.actionBar);
setSupportActionBar(actionBar);

  1. 执行此操作后,onCreateOptionsMenu 中添加的所有操作都将添加到工具栏,并将被视为活动操作栏.
  2. 根据需要进一步自定义工具栏(添加子视图等)
  1. Once you do that, all actions added in onCreateOptionsMenu will be added to the toolbar and it will be treated as the activity action bar.
  2. Further customize the Toolbar as desired (Add child views etc.)

这篇关于Android Lollipop、AppCompat ActionBar 自定义视图不占用整个屏幕宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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