ImageView中的填充不起作用

时间:2013-11-24 07:51:02

标签: android android-layout imageview padding

我想制作一个包含顶栏和图像的屏幕。我使用TextView作为顶部栏,使用ImageView作为图像。我想仅将填充设置为图像视图。我的xml如下。填充操作无效。任何人都可以解释为什么和做什么?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    android:background="@drawable/background" >

    <TextView android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/home"
        android:textSize="20sp"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:shadowColor="#000000"
        android:shadowRadius="1"
        android:shadowDy="-1" 
        android:gravity="center"
        android:background="@drawable/navBar"/>

    <ImageView android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:layout_below="@+id/topBar"        
        android:background="@drawable/image"/>

</RelativeLayout>

3 个答案:

答案 0 :(得分:13)

您应该使用layout_margin代替padding,因此应该如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"   
android:background="@drawable/background" >

<TextView android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/home"
    android:textSize="20sp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:shadowColor="#000000"
    android:shadowRadius="1"
    android:shadowDy="-1" 
    android:gravity="center"
    android:background="@drawable/navBar"/>

<ImageView android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"   
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:layout_below="@+id/topBar"        
    android:background="@drawable/image"/>

您可以为所有路线指定1 layout_margin,而不是使用

android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"

你可以使用:

android:layout_margin="10dip"

希望这就是你要找的东西。否则给我反馈;)

<强> 更新

您也可以设置cropToPadding

<ImageView 
...
   android:cropToPadding="true"
   android:padding="15dp"
   android:scaleType="centerInside"  
...

答案 1 :(得分:11)

在ImageView上使用

android:src="@drawable/image"

而不是

android:background="@drawable/image"

src标记用于表示填充,而背景标记则不用。 在某些情况下,尤其是在可点击的图标上,使用layout_margin而不是填充是个坏主意,因为填充属于视图并使点击区域更大!。

答案 2 :(得分:6)

唯一的解决方案是将imageview添加到另一个容器中。

 <FrameLayout
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginRight="10dp"
        android:padding="10dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@drawable/ic_delete_blue" />
    </FrameLayout>
相关问题