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系统工程师,然后在面试的时候对方的技术总监问了我 ...
随机推荐
- 廖雪峰Java10加密与安全-3摘要算法-3SHA-1算法
1.SHA-1算法 SHA-1算法也是一种哈希算法. 输出160 bits/20bytes 由美国国家安全局开发 SHA-0/SHA-1/SHA-256/SHA-512 * SHA-0有问题,已经作废 ...
- spring cloud深入学习(七)-----配置中心git示例
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...
- jeecms 修改后台访问路径
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_30553235/article/details/74971414 我使用的是jeecms ...
- Dock镜像初探索
一.安装CentOS版DockerCE 1.1 卸载旧的版本 yum remove docker \ docker-client \ docker-client-latest \ docker-com ...
- jqGrid 属性、事件全集
<html> ... <table id="list1"></table> <div id="pager1">& ...
- HDFS FsImage文件
- JDBC连接整个过程
1.导入驱动(放在lib下) connector-java-5.0.8-bin.jar 2.导入配置文件(放在src下) jdbc.properties driverClass=com.mysql.j ...
- 【html、CSS、javascript-12】jquery-效果
一.jQuery 效果- 隐藏和显示 通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素: $("#hide").click(func ...
- Django项目:CRM(客户关系管理系统)--24--16PerfectCRM实现King_admin日期过滤
登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html list_filter = ('date','source','consultant','con ...
- bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 原来觉得和 bzoj4977跳伞求生 有点像(虽然还没做). 所以对于a[ ]从小到大 ...