Placing/Overlapping (z-index) a view above actionbar tabs

牧云@^-^@ 提交于 2019-12-08 15:59:23

问题


I have a View that draws something outside the Fragment containing it and I configured it to draw the content outside it using this.

The problem is that it works everywhere but on the ActionBar and ActionBar Tabs.

mActionBar.addTab(
    mActionBar.newTab()
        .setCustomView(t));

I am using appCompat and adding tabs this way:

I added android:clipChildren="false" to all the parent Views, but it doesn't work just for ActionBar and ActionBar tabs.

The desired view:

But the result is this:


回答1:


It's not easy to help you without the complete source code; anyway I can suggest you an alternative approach which may suit your case.

If you are creating a custom floating item (maybe used for a tutorial or an hint), you should avoid to alter the base views hierarchy and prefer a pure overlay solution.

The PopupWindow is exactly what you need:

A popup window that can be used to display an arbitrary view.

Since the PopupWindow acts at Activity level, it should overlay everything in your current Activity.

The popup window is a floating container that appears on top of the current activity.

You can find a simple example here.




回答2:


I finally Removed the tabBar and replaced it with simple buttons to switch between tabs and placed it before CustomLayout in xml to be drawn before it.

<LinearLayout
    ....
    // tabs layout />

<CustomLayout

....
/>



回答3:


This happens because of hierarchical order of your activity layout. Your ActionBar is being drawn over your View. You can find your activity's frame and add the view there.

private void addCircleView() {
    final FrameLayout frameLayoutRoot = (FrameLayout) getActivity().getWindow()
        .getDecorView().findViewById(android.R.id.content);

    View circleView = inflater.inflate(
        R.layout.my_circle_view, frameLayoutRoot, false);

    ViewGroup.MarginLayoutParams marginLayoutParams =
        ((ViewGroup.MarginLayoutParams) circleView.getLayoutParams());

    marginLayoutParams.topMargin = getStatusBarHeight(getActivity()) 
        + getActivity().getActionBar().getHeight()
        + getResources().getDimensionPixelSize(R.dimen.your_margin_top_circle);

    circleView.setLayoutParams(marginLayoutParams);

    frameLayoutRoot.addView(circleView);
}

public int getStatusBarHeight(Context context) {

    int result = 0;

    final int resourceId = context.getResources().getIdentifier(
        "status_bar_height", "dimen", "android");

    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

Also remember to remove the view when you navigate away to an other fragment.

frameLayoutRoot.removeView(circleView);

Edit:

Please note that this is something that you should be doing at the level that you add ActionBar, which is the activity. In that case you wouldn't need these workarounds. This stuff are way more simple to achive with ToolBar.



来源:https://stackoverflow.com/questions/32735119/placing-overlapping-z-index-a-view-above-actionbar-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!