Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 将ImageView放在中心,宽度和高度是父宽度的一半?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将 ImageView放在ViewGroup(最好是RelativeLayout)中,如下所示. View组伸展以获取活动的整个宽度和高度.我想确保无论设备宽度是多少,ImageView的高度和宽度都应该是它的一半.

╔═══════════════════════╗   
║                       ║ 
║       View Group      ║ 
║                       ║ 
║    ╔═════════════╗    ║   
║    ║             ║    ║   
║    ║ ImageView   ║    ║   
║    ║             ║    ║   
║    ║ width = X/2 ║    ║   
║    ║ height= X/2 ║    ║   
║    ╚═════════════╝    ║   
║                       ║ 
║                       ║ 
║                       ║ 
╚═══════════════════════╝   
<----------------------->
Xpx width of Device/Viewgroup

有人可以请帮助我如何实现这一目标?

解决方法

也许尝试这样的事情:

public class CenteredImageView extends ImageView {


public CenteredImageView(Context context) {
    super(context);
}

public CenteredImageView(Context context,AttributeSet attrs) {
    this(context,attrs,0);
}

public CenteredImageView(Context context,AttributeSet attrs,int id) {
    super(context,id);
}

@Override
public void onMeasure(int measuredWidth,int measuredHeight) {
    super.onMeasure(measuredWidth,measuredHeight);
    ViewGroup parent = (ViewGroup) getParent();
    if(parent != null){
        int width = parent.getMeasuredWidth() / 2;
        setMeasuredDimension(width,width);
    }
}

可能没有必要对父进行空检查,但应相应地调整图像大小

大佬总结

以上是大佬教程为你收集整理的android – 将ImageView放在中心,宽度和高度是父宽度的一半?全部内容,希望文章能够帮你解决android – 将ImageView放在中心,宽度和高度是父宽度的一半?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。