除去多余的空间自定义动作条 [英] Removing Extra Space in Custom ActionBar

查看:111
本文介绍了除去多余的空间自定义动作条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用完全自定义的动作条意见(因为它是必要为我的客户怎么想的动作条的工作),我的问题的关注。我已经删除了标识,名称,以及其他一切可能的。然而,对于在ActionBar定制视图不会一路延伸穿过屏幕。

My issue concerns using fully custom ActionBar views (as it's necessary for how my customer wants the ActionBar to work). I've removed the logo, title, and everything else possible. However, the custom view for the ActionBar will not extend all the way across the screen.

我已经试过以下(作为一个快速概述):

I've tried the following (as a quick overview):

  • 的动作条(片段下同)动态删除方面
  • 删除选项菜单的全部
  • 指定的样式/主题,从动作条中删除一切

下面是我的问题的截图:

Here is a screenshot of my issue:

下面是样式:(作为一个方面说明,我原来一直本地动作条,但除去大部分)

Here is the style: (as a side note, I originally kept the native ActionBar but removed most of it)

<!-- Customized App Theme -->
<style name="AppTheme2" parent="Theme.AppCompat.Light">
    <!-- Let the actioBbar overlay the activity, ie activity is full screen -->
    <item name="android:windowActionBarOverlay">true</item>
    <!-- Set up the action bar styles -->
    <item name="android:actionBarStyle">@style/ActionBarEmpty</item>
    <!-- Remove the shadow under the actionBar -->
    <item name="android:windowContentOverlay">@null</item>

    <!-- Set the drawer icon to show up instead of the "Up" caret icon
    <item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
    <item name="homeAsUpIndicator">@drawable/ic_drawer</item>
    -->

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/ActionBarEmpty</item>
</style>

<!-- Final, empty Action Bar style | makes space for customized actionBar -->
<style name="ActionBarEmpty" parent="Widget.AppCompat.Base.ActionBar">
    <!-- TODO: Add support versions -->
    <item name="android:displayOptions">none</item>
    <item name="android:background">@color/transparent</item>

    <!-- Tested the below, does absolute nothing -->
    <item name="android:layout_margin">0dp</item>
    <item name="android:padding">0dp</item>
    <item name="android:minWidth">0dp</item>
</style>

下面是code。在我的活动中,人们关注建立动作条:

Here is the code in my Activity that concerns setting up the ActionBar:

// Initialize and set up the ActionBar
mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);

// Set the actionBar layout initially to the simple one
mActionBar.setCustomView(mViewActionBarSimple);

下面是截图为动作条显示的看法:

Here is the view shown in the screenshot for the ActionBar:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="?android:actionBarSize"
  android:background="@color/white">
    <ImageButton
        android:id="@+id/navButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/padding_actionbar_icon"
        android:paddingBottom="@dimen/padding_actionbar_icon"
        android:paddingRight="@dimen/padding_actionbar_icon"
        android:paddingEnd="@dimen/padding_actionbar_icon"
        android:layout_marginLeft="@dimen/margin_actionbar_icon_left"
        android:layout_marginStart="@dimen/margin_actionbar_icon_left"
        android:src="@drawable/ic_drawer_normal"
        android:background="?android:attr/selectableItemBackground"
        />
    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_general"
        android:layout_gravity="center"
        android:textColor="@color/black"
        android:textSize="@dimen/text_normal"
        android:text="@string/garbage_fill_short"
        />
</LinearLayout>

在除了试图消除一切我能想到的,我也试图删除选项菜单:

In addition to trying to remove everything I could think of, I also tried to remove the options menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return false;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // To remove the options menu - and make the custom actionBar take the full space -
        // always return false
    return false;
}

我在三星Galaxy S4采用Android奇巧运行此。

I'm running this on a Samsung Galaxy S4 with Android KitKat.

感谢您阅读本。任何帮助将是pciated此时AP $ P $。

Thank you for reading this. Any help would be appreciated at this point.

更新:对于任何人读这篇文章,我不知道为什么它的工作是这样,但是我来到了以下问题的答案如下之后。我包围了动作条的布局与RelativeLayout的[与LinearLayout中,其内容为儿童],且宽/背景固定本身?答案仍然是惊人的作为非常明确的,绝对消除了动作条,而在此之前我是仍在使用它以某种方式。

Update: For anyone reading this, I don't know why it worked like this, but I came upon the problem after following the below answer. I surrounded the actionBar layout with a RelativeLayout [with the LinearLayout and its contents as the children], and the width/background fixed itself... The answer is still amazing as its very clear cut and absolutely removes the actionBar, while before I was still using it in some way.

推荐答案

请尝试以下通过创建活动的基础,所以你可以在所有的应用程序与放大器参考删​​除操作栏的内容;从布局膨胀的自定义操作栏

Please try below for removing action bar content by creating base activity so you can refer in all your app & inflate custom action bar from layout

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
            getSupportActionBar().hide();
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setHomeButtonEnabled(false);

            actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            View homeIcon = findViewById(android.R.id.home);
            // Hides the View (and so the icon)
            if (homeIcon != null)
                ((View) homeIcon.getParent()).setVisibility(View.GONE);

            overridePendingTransition(0, 0);
    }

添加自定义视图

add custom view

@SuppressLint("InlinedApi")
@Override
public void setContentView(int layoutResID) 
{
    super.setContentView(R.layout.activity_base);
    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
    viewGroup.removeAllViews();
    viewGroup.addView(getLayoutInflater().inflate(layoutResID, null));
    // you can find action_bar layouts view & add listner
}

和自定​​义XML包含activity_base.xml自定义操作栏

and customise XML containing custom action bar in activity_base.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:orientation="vertical" >

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

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right" />

</LinearLayout>

如果你想用再延长这一活动。

Where you want to use then extend this activity.

这篇关于除去多余的空间自定义动作条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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