Android删除Actionbar上的边距/填充

时间:2015-06-07 13:09:04

标签: android android-layout android-viewpager tabview

我有一个Android应用程序,它有一个透明的ActionBar和所有其他系统栏使用true等等。以下是我使用自定义TabView时的样子,我将其指定为Actionbar的自定义视图。 image

如何删除红色条带和屏幕左侧之间的空白区域?

我的主要活动:

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

我的应用主题代码:

<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>

我的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" />

我的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);
    }
}

}

1 个答案:

答案 0 :(得分:0)

重点是用工具栏本身做一些事情。这是代码

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