自定义高度与宽度都相等的RelativeLayout
来源:互联网 发布:写作文软件app 编辑:程序博客网 时间:2024/05/31 19:23
最近公司的项目有个页面需要做成类似与微信发表动态时提醒谁看的效果:
GridView的item是正方形,而android需要适配不同尺寸的手机,所以不能写死item的高度和宽度。因为GridView是可以根据numColumns来分配item的宽度的,所以我们把item布局的高度设为与宽度一样就能实现适配了。
自定义高度与宽度都相等的RelativeLayout:
/** * Created by Administrator on 2016/9/27 0027. * 高与宽相等的RelativeLayout */public class SquareLayout extends RelativeLayout { public SquareLayout(Context context) { super(context); } public SquareLayout(Context context, AttributeSet attrs) { super(context, attrs); } public SquareLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // For simple implementation, or internal size is always 0. // We depend on the container to specify the layout size of // our view. We can't really know what it is since we will be // adding and removing different arbitrary views and do not // want the layout to change as this happens. this.setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec)); // Children are just made to fill our space. int childWidthSize = getMeasuredWidth(); //设置高度与宽度一样 heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}item的布局:
<?xml version="1.0" encoding="utf-8"?><com.yifan.viewmine.SquareLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/civ" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@mipmap/duck" /></com.yifan.viewmine.SquareLayout>activity的代码就不贴出来了。
最后上效果图:
GridView的宽度设小点:
1 0
- 自定义高度与宽度都相等的RelativeLayout
- 关于二叉树的宽度,高度,相等
- 关于二叉树的宽度,高度,相等
- Android自定义RelativeLayout,使宽度和高度一样
- 自定义imageview使图片高度和宽度相等
- Android GridView设置条目的高度与宽度相等、GridView条目宽高相同
- Android GridView设置item的高度与宽度相等、GridView条目宽高相同
- 树的宽度与高度
- css 宽度与高度按百分比显示,且宽高相等,span高度百分比
- 如何自定义pickerView的高度和宽度!
- 自定义按钮的标题宽度、高度
- 设置和获取RelativeLayout高度和宽度
- 自定义控件-宽度固定 高度随宽度变化的图片
- 长宽相等的RelativeLayout
- 代码笔记 | 宽度与屏幕相等,高度根据图片拉伸后做适配
- 做项目中遇到一个小问题 (要让容器与浏览器宽度,高度相等)
- android布局时,左侧宽度固定而右侧textview高度变化,怎么9png实现左侧高度与右侧相等?
- 常见的宽度与高度获取
- 数据结构实验之栈八:栈的基本操作
- 2016弱校连萌十一专场10.3 部分题解
- 数据库之三范式
- web.xml中 error-page的正确用法
- linux挂载与卸载 mount umount
- 自定义高度与宽度都相等的RelativeLayout
- 磕磕绊绊,Spring+Mybatis多数据源配置mysql+oracle
- js垃圾回收机制
- 从留言板开始做网站(七)——服务端的数据验证和安全输入
- MyBatis浅谈
- Java面向对象之泛型
- .bat文件详解
- 调制:调幅(AM)与调频(FM)
- 数据库常用命令概括