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系统工程师,然后在面试的时候对方的技术总监问了我 ...
随机推荐
- List -- 变更列表
1,一些常见的内建函数 L.append # 加一个 L.extend # 加一串 L.insert(index, item) #固定位置插入 L.[index : index] = sequence ...
- 廖雪峰Java10加密与安全-4加密算法-2口令加密算法
对称加密key是一个byte数组,如AES256算法的key是一个32字节的数组,普通的加密软件由用户输入加密口令.如果由用户输入口令,进行加密/解密,需要用到PBE算法. 1.PBE:Passwor ...
- HZOI 可怜与超市 树形dp
学长留的题,质量还是灰常高的. 而且我树规本身较弱,一道也不想放下 题目链接:https://www.cnblogs.com/Juve/articles/11203824.html 题解:这道题我们可 ...
- sql错误;The user specified as a definer ('tester'@'%') does not exist
在复制和导数据库时提示错误:SELECT command denied to user 'tester'@'%' for column 'uID' in table 'view_enterprise_ ...
- spring cloud深入学习(十)-----配置中心和消息总线(配置中心终结版)
如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...
- css3之文本和颜色功能之text-shadow
总本看一下 1.text-shadow 语法:text-shadow: h-shadow v-shadow blur color; h-shadow: 必需.水平阴影的位置.允许负值. v-shado ...
- jQuery 取值、赋值的基本方法整理
/*获得TEXT.AREATEXT的值*/ var textval = $("#text_id").attr("value"); //或者 var textva ...
- IDEA中配置国内maven库镜像(阿里云)
配置maven中settings.xml,在mirrors段内增加镜像配置,配置后如下: <mirror> <id>nexus-aliyun</id> <mi ...
- svn: E170013: Unable to connect to a repository at URL svn: E230001: Server SSL certificate verification
idea更新项目报E230001: Server SSL certificate verification failed: certificate issued for a different hos ...
- 【xlwings1】多线程写入excel数据
#!/ufr/bin/env python # -*- coding:utf-8 -*- import xlwings as xw import queue import threading impo ...