我目前正在开发一个包含图像视图的Android小部件.
该小部件应该可以调整大小.
图像应该填充小部件的整个宽度(这是容易的部分)并且应该填充小部件高度的30%,同时尊重最大高度80dip(这是我无法弄清楚的).
在以下布局中,图像适当缩放到宽度,但不适合高度:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/Widget" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:padding="@dimen/Widget_margin"> <ImageVIEw androID:ID="@+ID/promoshelf" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:src="@drawable/shelf" androID:scaleType="fitXY" androID:layout_alignParentBottom="true" androID:layout_centerHorizontal="true" /></relativeLayout>
我尝试使用带权重的linearLayout来完成此 *** 作,这可以在下面的代码片段中看到.但是,它看起来像androID:maxHeight在使用权重时不受尊重.
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/Widget" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:padding="@dimen/Widget_margin"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:gravity="bottom" androID:orIEntation="vertical" androID:weight_sum="1"> <ImageVIEw androID:layout_wIDth="match_parent" androID:layout_height="0dip" androID:maxHeight="80dip" androID:src="@drawable/my_image" androID:scaleType="fitXY" androID:layout_weight="0.3" /> </linearLayout></relativeLayout>
所以,这是(不需要的)结果:
为什么maxHeight不起作用或者是否有人知道如何规避这个?
解决方法:
我不确定是否有一个只使用标准组件的好解决方案.但是可以使用自定义ImageVIEw小部件轻松完成.
public class MyImageVIEw extends ImageVIEw { private float weight = 0.3f; private int maxHeight; public MyImageVIEw(Context context, AttributeSet attrs) { super(context, attrs); if (attrs != null) { final int[] IDs = {androID.R.attr.maxHeight}; final TypedArray array = context.obtainStyledAttributes(attrs, IDs); if (array != null) { maxHeight = array.getDimensionPixelSize(0, 0); array.recycle(); } } } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) { if (maxHeight > 0) { final int height = MeasureSpec.getSize(heightmeasureSpec); final int result = Math.min(Math.round(height * weight), maxHeight); heightmeasureSpec = MeasureSpec.makeMeasureSpec(result, MeasureSpec.EXACTLY); } super.onMeasure(wIDthMeasureSpec, heightmeasureSpec); }}
然后在你的布局中:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#0f0"> <com.example.MyImageVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_alignParentBottom="true" androID:background="#f00" androID:maxHeight="80dip"/></relativeLayout>
总结 以上是内存溢出为你收集整理的android – 如何根据小部件高度缩放图像视图但是尊重最大高度?全部内容,希望文章能够帮你解决android – 如何根据小部件高度缩放图像视图但是尊重最大高度?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)