android 显示内容被底部导航栏遮挡

本文介绍了一种解决Android应用中EditText界面全屏显示时底部导航栏遮挡问题的方法。通过在style.xml中配置特定参数,使界面适配不同设备,并提供了一个辅助类AndroidBug5497Workaround来解决软键盘弹出时布局调整的问题。

描述:

由于产品需求,要求含有EditText的界面全屏显示,最好的解决方式是使用AndroidBug5497Workaround.assistActivity(this)
的方式来解决,但是华为和魅族手机系统自带的有底部导航栏,会造成一些布局被遮挡。

解决方案:values-21的style.xml中添加android:windowDrawsSystemBarBackgrounds”并将值设置为false,方式如下

  • 在style引用的主题里面加入android:windowDrawsSystemBarBackgrounds”并将值设置为false,会自动提醒点击alt+Enter会新建values-21的文件夹并生成styles.xml的文件。
  • 也可以自己忽略的,直接新建values-21的文件夹然后新建一个styles.xml的文件,将主题里面的内容复制到styles.xml里面然后加上加入android:windowDrawsSystemBarBackgrounds”并将值设置为false即可解决。


例外:附上(网上找到的)
public class AndroidBug5497Workaround {

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content 

view set.

    public static void assistActivity (Activity activity) {
        new AndroidBug5497Workaround(activity);
    }
    private Activity activity;
    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        this.activity = activity;
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new 

ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        LogUtils.e("possiblyResizeChildOfContent","usableHeightNow:"+usableHeightNow);
        LogUtils.e("possiblyResizeChildOfContent","usableHeightPrevious:"+usableHeightPrevious);
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();

            //这个判断是为了解决19之前的版本不支持沉浸式状态栏导致布局显示不完全的问题
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
                Rect frame = new Rect();
                activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
                int statusBarHeight = frame.top;
                usableHeightSansKeyboard -= statusBarHeight;
            }
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);

        //这个判断是为了解决19之后的版本在弹出软键盘时,键盘和推上去的布局(adjustResize)之间有黑色区域

的问题
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            return (r.bottom - r.top)+statusBarHeight;
        }

        return (r.bottom - r.top);
    }

}


<think>根据用户需求,需要实现Android底部导航栏的凸起效果(例如中间按钮凸起)。这种效果通常通过自定义布局和背景实现。以下是一种常见的实现方法: ### 实现步骤 1. **创建自定义底部导航栏布局**:使用`LinearLayout`或`RelativeLayout`作为容器,包含多个按钮(通常为`ImageButton`或`TextView`配合图标)。 2. **设计凸起按钮**:将中间的按钮设置为一个凸起的圆形图标,并调整其位置(通常使用负margin上移)。 3. **处理点击事件**:为每个按钮设置点击事件,切换选中状态。 4. **处理导航栏遮挡问题**:确保内容不被系统导航栏遮挡。 ### 详细代码示例 #### 布局文件(例如:`bottom_navigation.xml`) ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <!-- 底部导航背景(一条横线) --> <View android:id="@+id/nav_background" android:layout_width="match_parent" android:layout_height="1dp" android:background="#e0e0e0" android:layout_alignParentTop="true"/> <!-- 凸起按钮(居中) --> <ImageButton android:id="@+id/center_button" android:layout_width="60dp" android:layout_height="60dp" android:background="@drawable/ic_raised_button" <!-- 圆形背景,带阴影 --> android:src="@drawable/ic_add" android:layout_centerHorizontal="true" android:layout_alignParentTop="true" android:layout_marginTop="-30dp" <!-- 上移一半高度,实现凸起 --> android:elevation="8dp"/> <!-- 左侧按钮 --> <LinearLayout android:id="@+id/left_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:gravity="center"> <ImageView android:id="@+id/left_icon" android:layout_width="24dp" android:layout_height="24dp" android:src="@drawable/ic_home"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" android:textSize="12sp"/> </LinearLayout> <!-- 右侧按钮(类似左侧,略) --> ... </RelativeLayout> ``` #### 处理系统导航栏遮挡 在`styles.xml`中设置窗口属性,防止内容被系统导航栏遮挡: ```xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 其他属性 --> <item name="android:windowDrawsSystemBarBackgrounds">false</item> <item name="android:windowTranslucentNavigation">false</item> </style> ``` [^1] #### 注意事项 - **凸起按钮的阴影**:使用`elevation`属性(API 21+)或阴影背景图(兼容低版本)。 - **点击事件处理**:为每个按钮设置点击监听器,切换选中状态并更新UI。 - **兼容性**:对于低于Android 5.0的设备,考虑使用兼容库或替代方案。 ### 相关问题 1. 如何为凸起按钮添加点击动画效果? 2. 如何实现底部导航栏的渐变背景? 3. 在Android中如何处理全面屏手机的底部导航栏适配? 引用[1]:android底部导航栏点击凸起,解决android 显示内容底部导航栏遮挡的问题。在style引用的主题里面加入android:windowDrawsSystemBarBackgrounds”并将值设置为false,会自动提醒点击alt+Enter会新建values-21的文件夹并生成styles.xml的文件。[^1]</think>### Android 底部导航栏凸起效果实现方法 要实现底部导航栏的凸起效果(如中间按钮凸起),可通过以下步骤完成: #### 1. **布局实现** ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <!-- 底部导航背景 --> <View android:id="@+id/nav_background" android:layout_width="match_parent" android:layout_height="56dp" android:background="#FFFFFF"/> <!-- 凸起按钮(居中) --> <FrameLayout android:layout_width="70dp" android:layout_height="70dp" android:layout_centerHorizontal="true" android:layout_alignParentTop="true" android:layout_marginTop="-25dp" <!-- 关键:负边距实现凸起 --> android:background="@drawable/raised_button_bg" android:elevation="8dp"> <ImageButton android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/ic_center" android:background="?attr/selectableItemBackgroundBorderless"/> </FrameLayout> <!-- 常规导航按钮(左右各两个) --> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:orientation="horizontal"> <!-- 左侧按钮组 --> <LinearLayout>...</LinearLayout> <!-- 占位空间(给凸起按钮留位置) --> <Space android:layout_width="70dp" android:layout_height="match_parent"/> <!-- 右侧按钮组 --> <LinearLayout>...</LinearLayout> </LinearLayout> </RelativeLayout> ``` #### 2. **凸起按钮背景(res/drawable/raised_button_bg.xml)** ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FFFFFF"/> <stroke android:width="1dp" android:color="#EEEEEE"/> <!-- 添加阴影 --> <corners android:radius="35dp"/> </shape> ``` #### 3. **解决系统导航栏遮挡问题** 在 `styles.xml` 中添加: ```xml <style name="AppTheme" parent="Theme.MaterialComponents.Light"> <item name="android:windowDrawsSystemBarBackgrounds">false</item> <item name="android:windowTranslucentNavigation">false</item> </style> ``` [^1] #### 4. **关键实现技巧** 1. **负边距**:`android:layout_marginTop="-25dp"` 使按钮向上凸出 2. **分层设计**: - 底层:背景条(高度56dp) - 中层:常规导航按钮 - 顶层:凸起按钮(使用`FrameLayout`) 3. **阴影效果**: - 使用 `android:elevation="8dp"`(API 21+) - 低版本兼容:添加阴影图层 4. **点击区域优化**: ```xml android:background="?attr/selectableItemBackgroundBorderless" ``` #### 5. **注意事项 - **兼容性**:使用 `marginTop` 负值而非 `translationY`,确保点击区域正确 - **阴影优化**:对API<21设备添加 `elevation` 兼容库 - **凸起高度**:建议不超过系统导航栏高度的50% - **状态管理**:使用 `View.setSelected()` 处理按钮选中状态 ### 效果图 ``` ------------------- | | 凸起 | | | ○ | ▢ | ○ | ------------------- ``` ### 相关问题 1. 如何为凸起按钮添加点击动画效果? 2. 在Jetpack Compose中如何实现底部导航凸起效果? 3. 如何处理凸起按钮与系统手势导航的冲突? 4. 如何优化凸起导航栏的内存占用? 引用[1]:android底部导航栏点击凸起,解决android 显示内容底部导航栏遮挡的问题。在style引用的主题里面加入android:windowDrawsSystemBarBackgrounds”并将值设置为false,会自动提醒点击alt+Enter会新建values-21的文件夹并生成styles.xml的文件。[^1]
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值