nestedScrollView下布局height<ScreenH-ToolbarH时的问题
2016-07-20 15:24
459 查看
这个问题困扰我很久了,在stack上找了好久,有好多解决方案似乎在我这都不起作用,今天总算借鉴了一些帖子搞出来了,谷歌总是留出一堆坑等着你跳啊。
首先上效果图:
上布局文件:
解决方案:
stack上好多人说写layout_gravity="fill_vertical"能解决,但是楼主亲测不行。不知道是不是我脸黑。。。我的解决方案是动态获取下面部分应有的高度,再动态赋值。
然后再5.1仍然下面多了一丝丝。。。。
--------------------------------------------------------------------------------------------
所以不能总是复制别人的代码,继续撸自己的代码!
修改后代码:
下面贴几个相关问题的解决方案:
点击打开链接
点击打开链接
点击打开链接
首先上效果图:
上布局文件:
<android.support.v4.widget.NestedScrollView 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:id="@+id/nestedScrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="fill_vertical" android:background="@color/fragment_background" tools:context=".fragments.DeviceStateFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- TODO: Update blank fragment layout --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView style="@style/MenuTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设备状态" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp"> <TextView style="@style/MenuItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="当前模式" /> <TextView android:id="@+id/currentMode_tv" style="@style/MenuItemValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="默认" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#666869" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp"> <TextView style="@style/MenuItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="运行状态" /> <TextView style="@style/MenuItemValue" android:id="@+id/runState_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="正常" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#666869" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView style="@style/MenuTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设备故障信息" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp"> <TextView style="@style/MenuItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="故障项" /> <TextView style="@style/MenuItemValue" android:id="@+id/faultItem_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="无" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#666869" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp"> <TextView style="@style/MenuItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="硬件各模块" /> <TextView style="@style/MenuItemValue" android:id="@+id/hardwareModule_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="无" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#666869" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView style="@style/MenuTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设备运行信息" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp"> <TextView style="@style/MenuItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="运行时间" /> <TextView style="@style/MenuItemValue" android:id="@+id/runDuration_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="2384h" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#666869" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp"> <TextView style="@style/MenuItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="当前检测次数" />] <TextView style="@style/MenuItemValue" android:id="@+id/currentDetechTimes_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="67545次" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#666869" /> </LinearLayout> </LinearLayout> </android.support.v4.widget.NestedScrollView>他的外层布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <include layout="@layout/toolbar_mainactivity" /> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/id_fragmentMain" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="com.nuctech.tr.trapp.behavior.FixScrollingFooterBehavior"></FrameLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" style="@style/floatingActionButtonStyle" /> </android.support.design.widget.CoordinatorLayout>
解决方案:
stack上好多人说写layout_gravity="fill_vertical"能解决,但是楼主亲测不行。不知道是不是我脸黑。。。我的解决方案是动态获取下面部分应有的高度,再动态赋值。
ViewGroup.LayoutParams layoutParams = nestedScrollView.getLayoutParams(); layoutParams.height = (int) getListHeight(); nestedScrollView.setLayoutParams(layoutParams);
public float getListHeight() { DisplayMetrics displayMetrics = getScreenSize(); float height = displayMetrics.heightPixels; float density = mContext.getResources().getDisplayMetrics().density; float headerHeight = 0; float screenHeight = height / density; int actonBarHeight = (int) (getActionBarHeight() / density); return (screenHeight - headerHeight) * density - actonBarHeight; } public int getActionBarHeight() { TypedValue tv = new TypedValue(); if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { return TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics()); } return -1; } public DisplayMetrics getScreenSize() { Display display = ((AppCompatActivity) mContext).getWindowManager().getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); display.getMetrics(outMetrics); return outMetrics; }
然后再5.1仍然下面多了一丝丝。。。。
--------------------------------------------------------------------------------------------
所以不能总是复制别人的代码,继续撸自己的代码!
修改后代码:
ViewGroup.LayoutParams layoutParams = nestedScrollView.getLayoutParams(); layoutParams.height = BaseApplication.getInstance().getScreenHeight() - BaseApplication.getInstance().getStatusBarHeight();
/** * 获得屏幕宽度 * * @param context * @return */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } /** * 获得状态栏的高度 * * @param context * @return */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; }
下面贴几个相关问题的解决方案:
点击打开链接
点击打开链接
点击打开链接
相关文章推荐
- linux下的守护进程
- 基于lucene的mr索引程序的实现
- 超强、超详细Redis数据库入门教程
- DPI 数据缺少-后台重新运行【./start2.sh 20160718 > start.log 2>&1 &】
- 【Java集合源码剖析】LinkedList源码剖析
- Unique Paths
- uc/os-iii学习笔记---同时等待多个内核对象
- Swift学习(六)-循环
- js:通过正则匹配获取页面的url中的参数
- CurlSharp
- es6学习笔记12--Class
- Shell脚本编程与文件系统修复
- 双边滤波原理
- Maven为web项目同时打jar和war包,deploy时只上传jar包的配置
- php两个数字进行比较大小
- <C++略识>之浅拷贝与深拷贝
- ServletContext容器,网站访问量
- 使用Toolbar + DrawerLayou实现菜单侧滑,改变toolbar左上角图标
- 【Android基础知识】使用Http和HttpClient上传文件
- linux源码Makefile详解