Android ImageView周围不需要的填充
我需要在我的所有活动/视图中包含标题图形。带有标头的文件称为header.xml:Android ImageView周围不需要的填充,android,user-interface,imageview,Android,User Interface,Imageview,我需要在我的所有活动/视图中包含标题图形。带有标头的文件称为header.xml: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:b
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:padding="0dip">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:padding="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:layout_gravity="fill"
android:background="#00FF00"
/>
</FrameLayout>
因此,当我实际尝试时,结果看起来像左边的图像,而不是它应该是什么样子(右):
(1) 这个-橙色-部分是有问题的图像/图像视图(2) 不受欢迎的绿色边框。注意:通常情况下,绿色区域是透明的-它只是绿色的,因为我设置了
背景
注意顶部图像周围的绿色边框;它是ImageView的一部分,我只是不知道它为什么会出现,或者我如何才能摆脱它。它将所有填充和边距设置为0(但忽略它们时结果相同)。该图像是480x64px jpeg*格式,我将其置于res/drawable格式(但不是drawable Xdpi
格式)
(*jpeg,因为我似乎偶然发现了老的png gamma问题-起初我解决了这个问题,将绿色边框设置为与图片相同的橙色,并且颜色不匹配。)
我在我的htc desire/2.2/Build 2.33.163.1和仿真器上试用了它。我还向安卓开发人员描述了这个问题;她可以重现这个问题,但也没有解释。构建目标是1.6
update@tehgoose:此代码产生完全相同的顶部+底部填充结果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
style="@style/white_background">
<!-- <include layout="@layout/header" /> -->
<ImageView
android:src="@drawable/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:layout_weight="0"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dip"
android:layout_weight="1">
(... rest of the elements)
</LinearLayout>
</LinearLayout>
(…其余要素)
您需要将其更改为“填充父对象”
您可能还需要缩放图像,使其成为填充框架布局的正确比例
我不明白你为什么需要在那里的框架布局。没有它,你的图像会填充屏幕
编辑:是的,对不起,xml是imageview的高度。可能是您可以给imageview指定的高度,例如50dp或40dp
,然后调整图像以显示绿色边框
例如:
android:layout\u height=“40dp”
您需要在imageview周围提供形状以提供更好的外观
以下是我使用的:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--<gradient android:startColor="#FFFFFF" android:endColor="#969696"
android:angle="270">
-->
<gradient android:startColor="#FFFFFF" android:endColor="#FFFFFF"
android:angle="270">
</gradient>
<stroke android:width="2dp" android:color="@color/graycolor" />
<corners android:radius="2dp" />
<padding android:left="5dp" android:top="5dp" android:right="5dp"
android:bottom="5dp" />
终于
<ImageView
(...)
android:adjustViewBounds="true" />
他们成功地做到了:
如果希望ImageView调整其边界以保留其可绘制视图的纵横比,请将其设置为true
我绊倒了。谢谢你的帮助 它与图像纵横比有关。默认情况下,系统保留图像源的原始纵横比。在大多数情况下,与布局或布局中指定的尺寸不完全匹配。所以,
android:scaleType
匹配图像。例如,您可以指定android:scaleType=“fitXY”
它对我很有用。在imageview xml中使用这个android:scaleType=“fitXY”这些额外的填充是自动生成的,因为android会尝试获取原始的纵横比。请在下面试试
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
唯一需要补充的是,他需要将布局高度设置为
ImageView
,这在您的回答中并不明显:)我也在没有周围布局的情况下尝试过,没有任何改变。此外,当前图像只是一个占位符-很快会有其他元素(证明周围布局合理)。我不希望图像与屏幕匹配,我只希望它在那里,在顶部。仔细检查图像的顶部和底部是否有空白。如果没有,请尝试使用android:scaleType=“”缩放imageview对象。应该有一个适合您所需的布局。设置布局\u width=“480px”和布局\u height=“64px”会产生我想要的结果,但像素不是很实用。我真的不明白这是如何转化为dp的。你应该像使用像素一样使用dp。这就是他们的全部;显示独立像素。dp/dip表示与密度无关的像素;据我所知,它涵盖了不同的密度,但(当然)屏幕尺寸并没有不同。我只想把图像缩放到100%的屏幕宽度,保留高度,这样比例就保持不变了。哇!我已经使用过形状(用于制表符背景),但老实说,我不明白应该如何在这里应用它。@Schnalle:创建两个shapes.xml文件和一个selector.xml文件。将图像背景应用为selector.xml文件。在选择器文件中,为聚焦/选中状态和正常状态提供两种状态。我也有同样的问题,但此解决方案没有效果。哇…如果没有该属性,则会根据屏幕密度添加不需要的填充,因此仅在某些设备上发生。这让我发疯,因为我拥有的所有设备上的图像看起来都很好,然后当我在朋友的设备上尝试时,它看起来像垃圾。谢谢你们。感谢上帝,我把我的第一个尝试从试图解决bug转变为寻找一些厌倦了android的东西。我相信这个问题是从一些制造商改变android的东西开始的,然后他们必须在下一个版本中设置一个属性来解决它。不幸的是,这不适用于较低的api,至少api 16和17。哇,这正是我要找的,我挠头了几个小时,甚至尝试使我的布局无效,并将其高度/宽度设置为图像的高度/宽度。这样一个简单的解决方案不需要scaleType就可以完美地工作。scaleType=“fitXY”
做了一些不同的事情:它改变了图像的纵横比,导致失真。这有时可能是可以接受的,例如,对于抽象背景图像。我使用安卓:宽度和安卓:高度控制纵横比,scaleType=“fitXY”确实使用所需的纵横比填充imageView,这至少是我的经验。感谢您的输入。它可以工作,但图像不精确、不平衡、裁剪不准确,但android:scaleType=“fitEnd”工作起来很神奇。我发现在布局中对齐图像视图时需要将android:adjustViewBounds=“true”
和android:scaleType=“fitXY”
组合在一起。Th
<ImageView
(...)
android:adjustViewBounds="true" />
android:scaleType="fitXY"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_height="wrap_content"