作者:mobiledu2502921033 | 来源:互联网 | 2023-07-21 18:14
ImusinganAppCompatDialogFragmenttoshowaBottomSheetDialog.ClickingthebackbuttonontheNa
I'm using an AppCompatDialogFragment
to show a BottomSheetDialog
. Clicking the back button on the Navigation Bar
closes the BottomSheetDialog
.
我正在使用AppCompatDialogFragment来显示BottomSheetDialog。单击导航栏上的后退按钮将关闭BottomSheetDialog。
I want to change the icon on the Navigation Bar
from the back button to the 'arrow-down' icon. This is by default done when the keyboard is shown and I want to replicate it for the Bottom Sheet.
我想将导航栏上的图标从后退按钮更改为“向下箭头”图标。默认情况下,这是在显示键盘时完成的,我想为底部工作表复制它。
To be clear, here what I have:
要清楚,我在这里:
And here is what I need:
这就是我需要的:
Note that the back button is an "arrow-down".
请注意,后退按钮是“向下箭头”。
The navigation-bar is a system-ui component and I don't see the way to change it's appearance to look like the navigation-bar displayed when the keyboard is visible.
导航栏是一个系统ui组件,我没有看到改变它的外观的方式,看起来像键盘可见时显示的导航栏。
3 个解决方案
8
In android O the concept of changing the icon is introduced but it is still through the 3rd party app. Custom Navigation Bar that uses WRITE_SECURE_SETTINGS
to change the icons. In Android O you can change the display of the bar i.e Light or Dark theme.
在Android O中,引入了更改图标的概念,但它仍然通过第三方应用程序。自定义导航栏使用WRITE_SECURE_SETTINGS来更改图标。在Android O中,您可以更改条形图的显示,即浅色或深色主题。
Solution 2 can be more of a help to you. You can create a popup window
on navigation Bar with the desired layout i.e 3 buttons back, Recent Apps and home button. In this way you can change the back button icon accordingly. Make sure the pop up window is of same height as the navigation Bar and you can then make your own functions for home and recent apps and in back function
you can close your BottomSheetDialog
and remove that popup window.
解决方案2可以为您提供更多帮助。您可以在导航栏上创建一个弹出窗口,其中包含所需的布局,即3个按钮,最近的应用程序和主页按钮。通过这种方式,您可以相应地更改后退按钮图标。确保弹出窗口与导航栏的高度相同,然后您可以为家庭和最近的应用程序创建自己的功能,在后退功能中,您可以关闭BottomSheetDialog并删除该弹出窗口。
The below is the code for home key as well as recent Apps. For back button do accordingly what you want to achieve with your own icon.
以下是主页密钥以及最近的应用程序的代码。对于后退按钮,请使用您自己的图标执行相应的操作。
For Home Button.
对于主页按钮。
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
For Recent Apps.
对于最近的应用。
Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClass.getMethod("getService", String.class);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerClass, "statusbar");
Class statusBarClass = Class.forName(retbinder.getInterfaceDescriptor());
Object statusBarObject = statusBarClass.getClasses()[0].getMethod("asInterface", IBinder.class).invoke(null, new Object[] { retbinder });
Method clearAll = statusBarClass.getMethod("toggleRecentApps");
clearAll.setAccessible(true);
clearAll.invoke(statusBarObject);
For Back Button // use your icon and function of closing the BottomSheetDialog.
对于后退按钮//使用关闭BottomSheetDialog的图标和功能。
For calculating the height of the navigationBar
用于计算navigationBar的高度
public static int getSoftButtonsBarSizePort(Activity activity) {
// getRealMetrics is only available with API 17 and +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}
you can also do it by adb
commands but make sure it can mess up your navigationBar
and you cannot get back your original navigationBar
.
您也可以通过adb命令执行此操作,但请确保它可能会弄乱您的navigationBar并且您无法取回原始的navigationBar。
I hope it helps.
我希望它有所帮助。