以缩放的ImageView为中心

时间:2014-03-03 00:14:15

标签: android xamarin

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

  <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>

1 个答案:

答案 0 :(得分:2)

加权导致你的问题。您在weightSum="1"上指定了LinearLayout,然后在layout_weight="0.5"上指定了ImageView。这意味着图像只会占用一半可用空间 - 前半部分,用于您在这里的布局。如果您将weightSum="2"和您的图片设置为layout_weight="0.5",则您的图片只会占用空间的四分之一(因为指定的重量是总数的1/4)。

您可以通过删除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>
相关问题