【问题标题】:Can't get an image (image view) under a transparent statusbar无法在透明状态栏下获取图像(图像视图)
【发布时间】:2019-05-22 17:31:05
【问题描述】:

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

谢谢。

我的主要活动:

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

【问题讨论】:

    标签: java android statusbar


    【解决方案1】:

    查看android:fitSystemWindows 和/或WindowInsets API。

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

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

    对于您的用例,android:fitSystemWindows 就足够了

    【讨论】:

    • 我已经发现并尝试使用它,但实际上没有任何改变
    • 在这种情况下,也许显示你尝试过的代码,这样更容易看到发生了什么。同样重要的是您的 minSdk 和 targetSdk 版本,以及您使用的库。例如。支持设计、appcompat 或相关库
    • 我用我认为重要的代码编辑了我的帖子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多