Android Lollipop、AppCompat ActionBar 自定义 View 不占用整个屏幕宽度

标签 android android-actionbar android-appcompat android-5.0-lollipop

所以,我刚刚将我的代码库更新为 Lollipop,但我遇到了操作栏的问题。我正在使用 AppCompat 和 ActionBarActivity,并为自定义 View 充气。好像自定义 View 不再占据整个屏幕宽度,在左边留下一条细长条

Building with 19 以前的样子

Building with 21 现在的样子

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

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

编辑

取出自定义 View 并更改背景现在占据了整个宽度。那么问题来了,我们如何才能让 CustomView 占据整个 ActionBar 的宽度呢?

最佳答案

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

我在阅读 ActionBar documentation 后遇到了同样的问题,尤其是下面的引用我找到了解决方案。

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 主题发生了变化,一方面似乎破坏了一些东西,但另一方面提供了更多的灵 active 。 我建议按照以下步骤操作:

  1. 在您的 Activity 中使用 .NoActionBar 样式,如上述引用所述
  2. android.support.v7.widget.Toolbar 添加到您的 Activity 布局中
  3. 设置 app:contentInsetStart="0dp" 属性。 这是您在问题中描述的边距的主要问题
<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>

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

<include layout="@layout/view_action_bar" />
  1. 在您的 Activity onCreate 中使用 findViewByIdsetSupportActionBar向 Activity 发出信号,哪个 Toolbar 应被视为 Activity 的操作栏
Toolbar actionBar = (Toolbar) findViewById(R.id.actionBar);
setSupportActionBar(actionBar);
  1. 一旦你这样做了,在 onCreateOptionsMenu 中添加的所有 Action 都将被添加到工具栏,并将被视为 Activity Action 栏。
  2. 根据需要进一步自定义工具栏(添加 subview 等)

关于Android Lollipop、AppCompat ActionBar 自定义 View 不占用整个屏幕宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26433409/

相关文章:

android - 更改不同选项卡的操作栏颜色

Android DatePicker 颜色样式

android - AppCompat MODE_NIGHT_AUTO 不工作

android - 如何使用 Android CameraX API 缩放相机?

Android getActionBar 与 getSupportActionBar?

java - 我将 getHostAddress() 与 try-catch 一起使用,但是当我运行它时,它只是不运行 这有什么问题吗?

android - 退出应用程序后在下拉导航菜单中保持选中的项目

java - Android 4.4 SDK 更新后 android-support-v7-appcombat jar 不匹配和应用程序崩溃

android - 按钮-声音-onHoverListener-老虎

android - EndlessAdapter 未将项目正确添加到自定义 ListView