Imageview未正确对齐

时间:2014-08-04 07:59:32

标签: android android-layout imageview

这就是我要做的事情:

enter image description here

这就是我所做的:

enter image description here

正如你可以看到袋鼠和图像之间存在一点差距。我完全不知道为什么会发生这种情况。帮助。

相关代码:

<RelativeLayout
         android:id="@+id/layout_image"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:layout_marginTop="40dp"
         android:layout_weight="0.1" >
        <ImageView //This is the kangaroo pic
             android:id="@+id/layout_image_fox"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/fox_holding_pic"


            />

        <LinearLayout
            android:id="@+id/box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_toRightOf="@+id/layout_image_fox"
            android:layout_marginLeft="1dp">
         <!--    android:paddingBottom="4dp"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:paddingTop="4dp"  -->

            <!-- android:layout_below="@+id/txt_scene_adress" -->

            <ImageView   //this is the footballer pic
                android:id="@+id/image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/shape"
                android:contentDescription="@string/desc_image"
                android:cropToPadding="true"
                android:src="@drawable/ic_launcher" 
                android:scaleType="centerCrop"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="32dp"
                 android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/> 

        </LinearLayout>

    </RelativeLayout>

更新

首先感谢大家的帮助。经过一些研究和一些答案,我已经更新了我的代码:

<RelativeLayout
         android:id="@+id/layout_image"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:layout_marginTop="40dp"
         android:layout_weight="0.1" >
        <ImageView 
             android:id="@+id/layout_image_fox"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/fox_holding_pic"


            />

        <LinearLayout
            android:id="@+id/box"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_centerInParent="true"
            android:paddingRight="4dp"
            android:paddingBottom="4dp"
            android:paddingTop="4dp" 
             android:layout_toRightOf="@+id/layout_image_fox" >

            <ImageView
                android:id="@+id/image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/shape"
                android:contentDescription="@string/desc_image"
                android:cropToPadding="true"
                android:src="@drawable/ic_launcher" 
                android:scaleType="centerCrop"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="32dp"
                android:layout_marginRight="5dp"
               />

        </LinearLayout>

    </RelativeLayout>

我的结果现在是:

enter image description here

足球运动员的形象仍然没有与袋鼠的图像一起剪裁。我应该使用框架布局还是应该从现有代码中改变一些东西?

4 个答案:

答案 0 :(得分:1)

可能是你的图像(袋鼠)左边有这个透明的边距导致了这个边距,你还为足球运动员的图像添加了剩余边距5dp。

<强>溶液

由于您已将android:cropToPadding设置为true,因此您可以使用android:layout_marginLeft的负值来定位该图像并将其剪切为袋鼠图像。你可以拥有与我不同的价值,只要确保你的位置符合你的喜好。

<ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="32dp"
        android:layout_marginLeft="-10dp" <--- negative value 
        android:layout_marginRight="5dp"
        android:layout_marginTop="16dp"

答案 1 :(得分:0)

设定重力

android:gravity="center"

到你的imageview并删除边距

答案 2 :(得分:0)

看看这是否有帮助..

<RelativeLayout
    android:id="@+id/layout_image"
    android:layout_gravity="center"
    android:layout_marginTop="40dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <ImageView //kangaroo
        android:id="@+id/layout_image_fox"
        android:layout_width="xyz"
        android:layout_height="abc"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="XYZdp"
    />
    <ImageView  ///footballer
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/layout_image_fox"
        android:contentDescription="@string/desc_image"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

答案 3 :(得分:0)

最后,我必须采用框架布局才能使其工作。所以狐狸的爪子与图像夹在一起:

代码:

  <FrameLayout
         android:id="@+id/layout_image"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:layout_marginTop="72dp"
         android:layout_weight="0.1">

        <LinearLayout
            android:id="@+id/box"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"> 

            <ImageView
                android:id="@+id/image_view"
                android:layout_width="220dp"
                android:layout_height="180dp"
                android:layout_marginBottom="32dp"
                 android:layout_marginLeft="65dp"
                android:background="@drawable/shape"
                android:contentDescription="@string/desc_image"
                android:cropToPadding="true"
                android:scaleType="fitXY"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>

        <ImageView
                android:id="@+id/layout_image_fox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/fox_holding_pic" 
                android:layout_marginLeft="-18dp"

                />

 </FrameLayout>

结果:

enter image description here