无法在透明状态栏下获取图像(图像视图)
Can't get an image (image view) under a transparent statusbar
我正在尝试在 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
。
看看 android:fitSystemWindows
and/or WindowInsets API 的。
fitSystemWindows
基本上告诉系统您希望您的视图适合整个屏幕。与默认情况相反,在默认情况下,您的视图只会呈现从状态栏下方到导航栏正上方的范围。
WindowInsets
可用于进一步 fine-tune 其中系统 UI 将或将不被绘制。
对于您的用例,android:fitSystemWindows
应该足够了
我正在尝试在 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
。
看看 android:fitSystemWindows
and/or WindowInsets API 的。
fitSystemWindows
基本上告诉系统您希望您的视图适合整个屏幕。与默认情况相反,在默认情况下,您的视图只会呈现从状态栏下方到导航栏正上方的范围。
WindowInsets
可用于进一步 fine-tune 其中系统 UI 将或将不被绘制。
对于您的用例,android:fitSystemWindows
应该足够了