无法在透明状态栏下获取图像(图像视图)

时间:2019-05-22 17:31:06

标签: java android statusbar

我正在尝试在Android Studio的状态栏下获取图像。到目前为止,我已经设法使状态栏完全透明,但似乎无法弄清楚如何在其下绘制图像。有人可以指出我正确的方向吗?

谢谢。

我的MainActivity:

public class MainActivity extends AppCompatActivity {
    private int newPosition = 0;
    private int currentPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new InfoFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;

                    switch (menuItem.getItemId()) {
                        case R.id.nav_info:
                            selectedFragment = new InfoFragment();
                            newPosition = 1;
                            break;
                        case R.id.nav_event:
                            selectedFragment = new EventsFragment();
                            newPosition = 2;
                            break;
                        case R.id.nav_shop:
                            selectedFragment = new ShopFragment();
                            newPosition = 3;
                            break;
                    }

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction transaction = fragmentManager.beginTransaction();

                    if (currentPosition < newPosition) {
                        //transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
                    } else if (currentPosition > newPosition) {
                        //transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_left);
                    }

                    transaction.addToBackStack(null);
                    transaction.add(R.id.fragment_container, selectedFragment, "FRAGMENT").commit();
                    currentPosition = newPosition;
                    return true;
                }
            };
}

我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_above="@id/bottom_navigation"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation"
        android:background="?android:attr/windowBackground"
        />
</RelativeLayout>

minSdkVersion为22,而targetSdkVersion为28

1 个答案:

答案 0 :(得分:0)

看看android:fitSystemWindows和/或WindowInsets API。

fitSystemWindows基本上告诉系统您希望视图适合整个屏幕。与默认视图相反,默认视图仅在视图从状态栏下方显示到导航栏上方的位置。

WindowInsets可用于进一步微调将要绘制或将不会绘制系统UI的位置。

对于您的用例,android:fitSystemWindows应该足够