我要制作一个包含顶部栏和图像的屏幕。我将TextView用于顶部栏并将ImageView用于图像。我只想将填充设置为 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>

最佳答案

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

10-07 19:35
查看更多