Android удаляет поля/отступы на панели действий

У меня есть приложение для Android с прозрачным ActionBar и всеми остальными системными панелями, созданными с использованием true и так далее. Вот как это выглядит, когда я использую пользовательский TabView, который я назначаю в качестве пользовательского представления для панели действий. изображение

Как я могу удалить это пустое пространство между красной полосой и левой стороной экрана?

Моя основная деятельность:

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

My MainActivity xml layout:

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

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

}


person Lukas Anda    schedule 07.06.2015    source источник
comment
Не могли бы вы опубликовать свой код для макета активности/фрагмента и макета панели инструментов?   -  person blueware    schedule 07.06.2015
comment
Хорошо, где вы используете TabBarView в своем xml, я не вижу в нем никакой пользы, не могли бы вы опубликовать весь код xml для своей основной деятельности?   -  person blueware    schedule 07.06.2015
comment
Это весь файл MainActivity XML. Я использую TabView в коде Java, я не определяю его в xml   -  person Lukas Anda    schedule 07.06.2015
comment
Я думаю, вам следует добавить свойство в xml основного действия для TabBarView, например: ‹com.yourapp.TabBarView android:id=@+id/tabBarView android:layout_width=fill_parent android:layout_height=fill_parent /› и затем ссылаться на него в вашей деятельности следующим образом: TabBarView tabBarView = (TabBarView ) findViewById(R.id.tabBarView), это может решить вашу проблему, поскольку вы будете управлять ее макетом из основного действия xml   -  person blueware    schedule 07.06.2015
comment
Эй, я нашел рабочее решение, я опубликую свой метод OnCreate, мне просто нужно было изменить смещение всей панели действий. Проверьте мой ответ   -  person Lukas Anda    schedule 07.06.2015


Ответы (1)


Смысл был в том, чтобы сделать кое-что с самой панелью инструментов. Вот код

@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);
    }
person Lukas Anda    schedule 07.06.2015