2014-03-03 70 views
0

我无法获得已缩小到我的应用程序中心的ImageView。我试过不同的scaleTypes(fitCenter,centerInside都给出了正确的尺寸,但都不居中),我试过使用RelativeLayout而不是LinearLayout,我尝试添加0.25的重量空视图到任何一方,我已经尝试将layout_width设置为特定的宽度,而不是使用layout_weight ...没有任何东西显示出来,图像只是左对齐。思考?居中缩放的ImageView

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:weightSum="1"> 
    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:layout_gravity="center" 
     android:src="@drawable/logo" 
     android:adjustViewBounds="true" 
     android:scaleType="centerInside" /> 
</LinearLayout> 

回答

2

权重造成你的问题在这里。您在您的LinearLayout上指定weightSum="1",然后在您的ImageView上指定layout_weight="0.5"。这意味着图像将只占用一半的可用空间 - 上半年,您在此处获得的布局。如果你设置了weightSum="2",你的图片为layout_weight="0.5",你的图片只占用四分之一的空间(因为分配的重量是总数的四分之一)。

您可以通过删除weightSumlayout_weight属性或完全删除LinearLayout来解决此问题。我建议删除LinearLayout,因为没有其他孩子,这是没有必要的;

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:src="@drawable/logo" 
    android:scaleType="centerInside" /> 

我误解。这个工作对我来说:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" /> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:adjustViewBounds="true" 
     android:scaleType="centerInside" 
     android:src="@drawable/logo" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" /> 

</LinearLayout> 
+0

重量是我如何缩放图像。这里的意图是图像占用宽度的50%。我尝试在图像周围放置空洞的视图,每个图像的重量为0.25,以尝试“填充”周围空间并将其强制到中间,但这也不起作用。 – Deeko

+1

我得到了空的意见工作(见编辑);我必须指定'0dp'的高度和宽度,并且我必须确保我的父级LinearLayout具有'orientation =“horizo​​ntal”'。 –

+0

啊!你是对的,它为空白视图添加了0dp高度。我的宽度为0dp,高度为match_parent。谢谢! – Deeko

-1

如果图像代码缩小(这我想是这样),比XML所有的设置都恢复为默认。您可以做的是在缩小图像后将重心设置为中心。 Here是如何。

+0

如果你在代码中建立一个'ImageView',这是真的,但是如果你通过'((ImageView)findViewById(R.id.myImageView)).setImageBitmap(...)'或相似,这是不正确的。 XML属性都保持不变。 –