nestedScrollView下布局height<ScreenH-ToolbarH时的问题

来源:互联网 发布:21天学通java这书怎样 编辑:程序博客网 时间:2024/05/16 12:02

这个问题困扰我很久了,在stack上找了好久,有好多解决方案似乎在我这都不起作用,今天总算借鉴了一些帖子搞出来了,谷歌总是留出一堆坑等着你跳啊。

首先上效果图:


上布局文件:

<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;    }




下面贴几个相关问题的解决方案:

点击打开链接

点击打开链接


点击打开链接


0 0
原创粉丝点击