下午做项目的时候,碰到页面布局需要使用RecycleView加HeaderView,本以为很简单东西,却有一个小小的坑:HeaderView布局的宽度不能撑满屏幕!

先看下出现问题的图:



天了噜,我写的布局代码可不是这样的,包括在as上面预览的时候,都是正常的,为何安装运行后变成了这样子。 先看下一开始写的布局代码和预览效果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical"
> <TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:gravity="left"
android:text="标题"
android:textColor="#333333"
android:textSize="30dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"> <View
android:layout_width="5dp"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:background="#D8D8D8" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="14dp"> <TextView
android:id="@+id/textDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:gravity="left"
android:lineSpacingExtra="3dp"
android:text="虽然刚刚九月,但我们已经迫不及待的要做一个2017年上映大片的超级前瞻。原因就是,值得期待的新片实在是太多了!我们列出了其中最热门大片、文艺小片和华语片,大家收藏好坐等上映吧!"
android:textColor="#707070"
android:textSize="18sp" /> </LinearLayout> </LinearLayout> </LinearLayout>



这布局效果,应该没错啊,可是咋运行到模拟器上就有问题了?

百思不得其解,google下,找到了一个解答,说是作为headerview的根布局不可以使用LinearLayout ,否则就会产生上述的问题。 具体原因呢,我没找到,估计需要看源码才能知道了,有兴趣的可以继续研究。。。

得知如何结局,那就好办了,把根布局改为RelativeLayout 就好了。

<?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="#FFFFFF"> <TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:gravity="left"
android:text="标题"
android:textColor="#333333"
android:textSize="30dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textTitle"
android:layout_marginBottom="40dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"> <View
android:layout_width="5dp"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:background="#D8D8D8" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="14dp"> <TextView
android:id="@+id/textDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:gravity="left"
android:lineSpacingExtra="3dp"
android:text="虽然刚刚九月,但我们已经迫不及待的要做一个2017年上映大片的超级前瞻。原因就是,值得期待的新片实在是太多了!我们列出了其中最热门大片、文艺小片和华语片,大家收藏好坐等上映吧!"
android:textColor="#707070"
android:textSize="18sp" /> </LinearLayout> </LinearLayout> </RelativeLayout>

再看下效果:

perfect!!!完美!!!

如有疑问或建议,欢迎进QQ群来讨论交流:487786925( Android研发村 )

Android RecycleView添加HeaderView宽度不能撑满屏幕问题的更多相关文章

  1. [Android]RecyclerView添加HeaderView出现宽度问题

    通过getItemViewType方式判断HeaderView方式添加HeaderView的,结果发现有几个界面HeaderView宽度不能满屏. 于是对比了几种布局,发现LinearLayout为根 ...

  2. vue基于 element-ui 实现菜单动画效果,任意添加 li 个数均匀撑满 ul 宽度

    %)%)%%%))) .) .) .) .) .) .) .) .) .) .) .) .) .) .) .) .% %% %deg);}

  3. Android 5.X新特性之为RecyclerView添加HeaderView和FooterView

    上一节我们讲到了 Android 5.X新特性之RecyclerView基本解析及无限复用 相信大家也应该熟悉了RecyclerView的基本使用,这一节我们来学习下,为RecyclerView添加H ...

  4. 解决EditText不能撑满全屏的问题及EditText你应该知道的属性

    一般我们要实现去下图一的效果很简单: 两个EditText就搞定 效果图一:   但是我们想让第二个EditText撑满剩余空间怎么做?如效果图二 效果图二: 解决: 使用了ScrollView嵌套L ...

  5. [Android] Android RecycleView和ListView 自定义Adapter封装类

    在网上查看了很多对应 Android RecycleView和ListView 自定义Adapter封装类 的文章,主要存在几个问题: 一).网上代码一大抄,复制来复制去,大部分都运行不起来,或者 格 ...

  6. SVG撑满页面

    当viewBox属性固定,默认修改svg标签的宽高,svg都会按比例缩放 我们现在不想按比例缩放,需要svg撑满整个画面 这里只需为svg标签添加一个关键属性:preserveAspectRatio ...

  7. Android课程---添加黑名单的练习2(课堂讲解)

    实现黑名单的添加.修改.查询和删除,首先得有封装的3个类,便于使用 BlackNumber.java package com.hanqi.test3; /** * Created by Adminis ...

  8. Android课程---添加黑名单的练习(课堂讲解)

    DBHelper.java package com.hanqi.test3; import android.content.Context; import android.database.sqlit ...

  9. Android下添加新的自定义键值和按键处理流程

            Android下添加新的自定义键值和按键处理流程     说出来不怕大家笑话,我写这篇博客的原因在于前几天去一个小公司面试Android系统工程师,然后在面试的时候对方的技术总监问了我 ...

随机推荐

  1. C语言函数指针和回调函数

    彻底搞定C指针-函数名与函数指针 函数名&函数名取地址 函数指针 通常我们可以将指针指向某类型的变量,称为类型指针(如,整型指针).若将一个指针指向函数,则称为函数指针. 函数名的意义 函数名 ...

  2. LUOGU P4113 [HEOI2012]采花

    传送门 解题思路 莫队题卡莫队...莫队只能拿到100分,满分200.正解主席树??发个莫队100分代码. 代码 #include<iostream> #include<cstdio ...

  3. QEventLoop配合QTimer实现阻塞任务超时处理

    A阻塞主线程正常运行,需要做特殊处理. 以下代码可实现,A阻塞或者正常处理时,均不阻塞主线程正常处理. QEventLoop eventloop; // use point to manage eve ...

  4. 软件-MQ-RabbitMQ:RabbitMQ

    ylbtech-软件-MQ-RabbitMQ:RabbitMQ RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语 ...

  5. python—时间与时间戳之间的转换

    python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...

  6. Size Balanced Tree(节点大小平衡树)

    定义 SBT也是一种自平衡二叉查找树,它的平衡原理是每棵树的大小不小于其兄弟树的子树的大小 即size(x->l)$\ge$size(x->r->l),size(x->r-&g ...

  7. jquery判断元素是否出现在可视区

      在我们的日常开发中,经常会遇到当元素出现在可视区的时候需要去出发某一事件的情况.我最近在优化环球网首页的时候,将非可视区的代码全部放入到webComponent中.打算当这个元素出现在可视区的时候 ...

  8. Python中的一些模块用法

    python中os模块用法 python之模块之shutil模块 -------------------------------os模块-------------------------------- ...

  9. sql 分页rownumber方式

    alter procedure [dbo].[proc_getpaging] ( ), --表名(可以为多表) ) = '*', --字段名(全部字段为*) ), --排序字段(支持多字段不用加ord ...

  10. npm常用命令及版本号

    npm 包管理器的常用命令 测试环境为node>=8.1.3&&npm>=5.0.3 1, 首先是安装命令 //全局安装 npm install 模块名 -g //本地安装 ...