2013-11-24 66 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代替填充是個不錯的主意,因爲填充屬於視圖並使點擊區域更大!