2013-11-24 65 views
6

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

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

回答

9

您应该使用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" 
... 
+0

是的,这正是我正在寻找的。感谢@Coderji为您的快速反应。 :) – CrazyLearner

+0

这是我的荣幸:),如果你发现这个有用的话,你可以打勾它作为正确的,所以人们可以在将来参考:) – Coderji

+1

我勾选它是正确的,但抱歉不能投票作为我的声誉得分低于15. :( – CrazyLearner

5

这个唯一的解决办法是添加的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> 
3

在ImageView的使用

android:src="@drawable/image" 

代替

android:background="@drawable/image" 

SRC标签荣誉的填充,而后台标签却没有。 在某些情况下,在可点击的图标上,使用layout_margin代替填充是个不错的主意,因为填充属于视图并使点击区域更大!