Android RecycleView添加HeaderView宽度不能撑满屏幕问题
下午做项目的时候,碰到页面布局需要使用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宽度不能撑满屏幕问题的更多相关文章
- [Android]RecyclerView添加HeaderView出现宽度问题
通过getItemViewType方式判断HeaderView方式添加HeaderView的,结果发现有几个界面HeaderView宽度不能满屏. 于是对比了几种布局,发现LinearLayout为根 ...
- vue基于 element-ui 实现菜单动画效果,任意添加 li 个数均匀撑满 ul 宽度
%)%)%%%))) .) .) .) .) .) .) .) .) .) .) .) .) .) .) .) .% %% %deg);}
- Android 5.X新特性之为RecyclerView添加HeaderView和FooterView
上一节我们讲到了 Android 5.X新特性之RecyclerView基本解析及无限复用 相信大家也应该熟悉了RecyclerView的基本使用,这一节我们来学习下,为RecyclerView添加H ...
- 解决EditText不能撑满全屏的问题及EditText你应该知道的属性
一般我们要实现去下图一的效果很简单: 两个EditText就搞定 效果图一: 但是我们想让第二个EditText撑满剩余空间怎么做?如效果图二 效果图二: 解决: 使用了ScrollView嵌套L ...
- [Android] Android RecycleView和ListView 自定义Adapter封装类
在网上查看了很多对应 Android RecycleView和ListView 自定义Adapter封装类 的文章,主要存在几个问题: 一).网上代码一大抄,复制来复制去,大部分都运行不起来,或者 格 ...
- SVG撑满页面
当viewBox属性固定,默认修改svg标签的宽高,svg都会按比例缩放 我们现在不想按比例缩放,需要svg撑满整个画面 这里只需为svg标签添加一个关键属性:preserveAspectRatio ...
- Android课程---添加黑名单的练习2(课堂讲解)
实现黑名单的添加.修改.查询和删除,首先得有封装的3个类,便于使用 BlackNumber.java package com.hanqi.test3; /** * Created by Adminis ...
- Android课程---添加黑名单的练习(课堂讲解)
DBHelper.java package com.hanqi.test3; import android.content.Context; import android.database.sqlit ...
- Android下添加新的自定义键值和按键处理流程
Android下添加新的自定义键值和按键处理流程 说出来不怕大家笑话,我写这篇博客的原因在于前几天去一个小公司面试Android系统工程师,然后在面试的时候对方的技术总监问了我 ...
随机推荐
- Odoo文档管理/知识管理应用实践 - 上传附件
测试环境: Odoo8.0 Odoo中的文档管理/知识管理可用于保存采购.销售.生产等一系列业务流程中产生的文件.凭证,可关联到具体的每一笔业务操作:也能用于管理公司的合同.资料,创建知识库以分享内部 ...
- python3 新特性
1.格式化字符串f-string user = "Mike" log_message = f'User{user} has logged in' 2.路径管理库Pathlib 3. ...
- vue.js_11_路由的2中参数传递和路由的嵌套
1.以?的形式传递参数 <router-link to="/login?id=10&name=zs">登录</router-link> 发送参数 ...
- sql server 获取指定格式的当前日期
使用sqlserver日期函数中的getdate()可以获取当现的日期,下面就将为您介绍这种使用sqlserver日期函数获取当前日期的方法. 但是如果我们只需要得到当前的日期,不需要时间部分,或者不 ...
- Python configparser的使用 读取配置文件
configparser是Python自带用于读取配置文件的工具包,它的使用非常简单 配置文件 配置文件[]中为section命名,section的命名可以包含空格,每个section下面以键值对的方 ...
- SSH免密登录设置
******配置主机之间的免密ssh登陆 假如 A 要登陆 B 在A上操作: (1)首先生成密钥对 ssh-keygen (提示时,直接回车即可) (2)再将A自己的公钥拷贝并追加到B的授权列 ...
- 【python之路36】进程、线程、协程相关
线程详细用法请参考:http://www.cnblogs.com/sunshuhai/articles/6618894.html 一.初始多线程 通过下面两个例子的运行效率,可以得知多线程的速度比单线 ...
- Django项目:CRM(客户关系管理系统)--50--41PerfectCRM实现全局账号密码修改
# gbacc_urls.py # ————————38PerfectCRM实现全局账号登录注销———————— from django.conf.urls import url from gbacc ...
- LUOGU P3435 [POI2006]OKR-Periods of Words
传送门 解题思路 首先求出kmp,那么i-nxt[i]一定是一个周期,对于每一个点一直跳nxt,跳到最小的nxt之后用i-这个nxt即为i这个前缀的答案. 代码 #include<iostrea ...
- [转]js的键盘事件
类型 键盘事件用来描述键盘行为,主要有keydown.keypress.keyup三个事件 keydown 当用户按下键盘上的任意键时触发,如果按住不放的话,会重复触发该事件 <div id=& ...