简体   繁体   English

Android在Actionbar上删除边距/填充

[英]Android remove margin/padding at Actionbar

I have an android app that has a transparent ActionBar and all other system bars made using true and so. 我有一个Android应用程序,它具有透明的ActionBar和使用true等制成的所有其他系统栏。 Here is how it looks like when I am using custom TabView which I assign as custom View to Actionbar. 这是我使用自定义TabView(将其作为自定义视图分配给Actionbar)时的外观。 图片

How can I remove that blank space between the red strip and the left side of the screen ? 如何删除红色条和屏幕左侧之间的空白?

My main activity: 我的主要活动:

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_main);
    LayoutInflater inflator =
            (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v = inflator.inflate(R.layout.custom_actionbar, null);
    tabBarView = (TabBarView) v.findViewById(R.id.tab_bar);

    v.setLayoutParams(new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT));

    ViewPager mPager = (ViewPager) findViewById(R.id.pager);

    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            tabBarView.setSelectedTab(position);
            tabBarView.setOffset(positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            tabBarView.setSelectedTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

    tabBarView.setStripHeight(20);
    tabBarView.setStripColor(getResources().getColor(android.R.color.holo_red_light));

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        // alternative to actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(v);
    }

    mPager.setAdapter(new MainPageAdapter(getFragmentManager()));

My app theme code: 我的应用主题代码:

<resources>
<style name="AppTheme1" parent="Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="android:windowActionBar">true</item>
</style>

<!-- ACTION BAR STYLES -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@drawable/actionbarbackground</item>
    <item name="android:windowActionBarOverlay">true</item>
    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbarbackground</item>
    <item name="windowActionBarOverlay">true</item>
</style>

My MainActivity xml layout: 我的MainActivity xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.digitale.mapped.MainActivity" />
<ImageButton
    android:id="@+id/newevent"
    android:layout_width="@dimen/fab_button_diameter"
    android:layout_height="@dimen/fab_button_diameter"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="40dp"
    android:layout_marginRight="@dimen/fab_button_margin_right"
    android:background="@drawable/fab_shape"
    android:contentDescription="New post"
    android:src="@drawable/fab_ic_add"
    android:tint="@android:color/white" />

My TabBarView Java code: 我的TabBarView Java代码:

public class TabBarView extends LinearLayout {

private static final int STRIP_HEIGHT = 6;

public final Paint mPaint;

private int mStripHeight;
private float mOffset;
private int mSelectedTab = -1;

public TabBarView(Context context) {
    this(context, null);
}

public TabBarView(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.actionBarTabBarStyle);
}

public TabBarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    mPaint = new Paint();
    mPaint.setColor(Color.WHITE);

    mStripHeight = (int) (STRIP_HEIGHT * getResources().getDisplayMetrics().density + .5f);
}

public void setStripColor(int color) {
    if (mPaint.getColor() != color) {
        mPaint.setColor(color);
        invalidate();
    }
}

public void setStripHeight(int height) {
    if (mStripHeight != height) {
        mStripHeight = height;
        invalidate();
    }
}

public void setSelectedTab(int tabIndex) {
    if (tabIndex < 0) {
        tabIndex = 0;
    }
    final int childCount = getChildCount();
    if (tabIndex >= childCount) {
        tabIndex = childCount - 1;
    }
    if (mSelectedTab != tabIndex) {
        mSelectedTab = tabIndex;
        invalidate();
    }
}

public void setOffset(float offset) {
    if (mOffset != offset) {
        mOffset = offset;
        invalidate();
    }
}

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    // Draw the strip manually
    final View child = getChildAt(mSelectedTab);
    if (child != null) {
        int left = child.getLeft();
        int right = child.getRight();
        if (mOffset > 0) {
            final View nextChild = getChildAt(mSelectedTab + 1);
            if (nextChild != null) {
                left = (int) (child.getLeft() + mOffset * (nextChild.getLeft() - child.getLeft()));
                right = (int) (child.getRight() + mOffset * (nextChild.getRight() - child.getRight()));
            }
        }
        canvas.drawRect(left, getHeight() - mStripHeight, right, getHeight(), mPaint);
    }
}

} }

The point was to do some stuff with toolbar itself. 关键是要使用工具栏本身来做一些事情。 Here is the code 这是代码

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_main);
    LayoutInflater inflator =
            (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v = inflator.inflate(R.layout.custom_actionbar, null);
    tabBarView = (TabBarView) v.findViewById(R.id.tab_bar);

    v.setLayoutParams(new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT));

    ViewPager mPager = (ViewPager) findViewById(R.id.pager);

    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            tabBarView.setSelectedTab(position);
            tabBarView.setOffset(positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            tabBarView.setSelectedTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

    tabBarView.setStripHeight(20);
    tabBarView.setStripColor(getResources().getColor(android.R.color.holo_red_light));

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
    //HERE COMES THE IMPORTANT PART
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(v);
        android.support.v7.widget.Toolbar parent = (android.support.v7.widget.Toolbar)v.getParent();
        parent.setContentInsetsAbsolute(0,0);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM