您可以通过创建自定义View并覆盖onMeasure()方法来解决此问题。如果您始终在xml中的layout_width中使用“ fill_parent”,则传递给onMeasusre()方法的widthMeasureSpec参数应包含父级的宽度。

public class MyCustomView extends TextView {

public MyCustomView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int parentWidth = MeasureSpec.getSize(widthMeasureSpec);

int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

this.setMeasuredDimension(parentWidth / 2, parentHeight);

}

}

您的XML看起来像这样:

android:layout_width="match_parent"

android:layout_height="match_parent">

class="com.company.MyCustomView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android如何基于父布局,如何根据父视图的尺寸调整Android视图的大小相关推荐

  1. Android 中LayoutInflater(布局加载器)之介绍篇

    本文出自博客Vander丶CSDN博客,如需转载请标明出处,尊重原创谢谢 博客地址:http://blog.csdn.net/l540675759/article/details/78099358 前 ...

  2. android:layout_marginleft的作用,当在FrameLayout中查看时,layout_marginLeft在Android API lt;11上正常工作...

    当我使用layout_marginLeft或从代码设置左边距时,它可以作为layout_marginRight使用.当我在FrameLayout中放置View with layout_marginLe ...

  3. android子view获取父布局,Android获取布局父ID(Android get layout parent id)

    Android获取布局父ID(Android get layout parent id) 我想知道View和ViewParent有什么区别? 我想获取ImageView父级的Id,但我不能这样做: m ...

  4. Android让VideoView填充满屏幕/父布局并保证不变形

    在Android中简单的播放一下视频选用VideoView是一个不错的选择,各个厂商在出厂的时候一般都会测试视频播放,这个兼容性一般都能得到保证,而且使用VideoView播放会自动的纠正视频角度,但 ...

  5. Android父布局上方上边框阴影实现。

    设置父布局背景 阴影本质上就是灰色的渐变,其中包括了颜色渐变和透明度渐变的组合. <?xml version="1.0" encoding="utf-8" ...

  6. android 父控件的背景_android控件拖动,移动、解决父布局重绘时控件回到原点

    /** * * @description 设置意见反馈,用以灰度发布 * @author zhongwr * @params * @update 2016年1月12日 下午5:36:07 */ pri ...

  7. 如何保证linearlayout_怎么让LinearLayout占据父布局的所有剩余空间

    LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失.因此一个垂直列表 ...

  8. Android ConstraintLayout 设置子 view maxWidth 是父 ConstraintLayout width 的百分比

    话不多说,直接上代码: <?xml version="1.0" encoding="utf-8"?> <androidx.constraint ...

  9. Flutter 图片如何充满父布局

    正常我们需要显示一张图片,会用到Image这个控件. 打个比方,我们加载一张本地的图片, 先看一下这个Image.asset的源码: Image.asset(String name, {Key key ...

最新文章

  1. MSDynamicsAX2009成本处理与重估(中文)
  2. Android 消息异步处理之AsyncTask
  3. python打卡记录去重_Python笔记记录
  4. [汇编语言]实验二:字的传送
  5. leetcode - 1191. K 次串联后最大子数组之和
  6. django2.x/3.x 前端页面在debug模式中找不到动态文件static
  7. [转]PHP echo, print, printf, sprintf函数的区别和使用
  8. Layui--颜色选择器layui.colorpicker
  9. small - HTML元素
  10. 如何在Linux系统上刷抖音
  11. 阿里巴巴创始人马云持股降至 4.8%;传小冰将从微软分拆,沈向洋“回归”;Electron 8.4.0 发布| 极客头条
  12. Machine Learning笔记(三) 多变量线性回归
  13. CentOS 安装Docker 并部署配置MySql
  14. Linux系统常用命令(一)
  15. matlab如何更改程序中所有同名_MATLAB作图实例:52:添加网格线并编辑位置
  16. 地籍测绘成图软件南方cass9.0支持AutoCad2010【安装文件和视频教程】
  17. 产品读书《自卑与超越》
  18. 吐槽表情包计算机系,网友用表情包形容自己的专业 分明是场吐槽大会
  19. 获取字符串中 图片路径
  20. “Improving Adversarial Robustness Requires Revisiting Misclassified Examples“ 论文笔记

热门文章

  1. SpringCloud五大神兽之Eureka服务注册(三)——Eureka的自我保护
  2. 怎么把PDF分割成几个文件?分享几种分割方法
  3. Python3+Selenium3+Pycharm自动化环境搭建(四):运行unittest时报错,unhandled inspector error
  4. 微信小程序 阻止事件冒泡
  5. imx6ul spi 设备驱动开发
  6. 关于浏览器部分JS失效原因
  7. Android点九图(.9.png)的特点和制
  8. 【Python进阶】9- Pandas的应用
  9. 新站长建设网站需要学习知识
  10. 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 HDU - 2191(特殊01背包)