ImageView 中的填充不起作用

新手上路,请多包涵

我想制作一个包含顶部栏和图像的屏幕。我将 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>

原文由 CrazyLearner 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 554
2 个回答

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

原文由 Coderji 发布,翻译遵循 CC BY-SA 3.0 许可协议

关于ImageView的使用

android:src="@drawable/image"

代替

android:background="@drawable/image"

src 标签支持填充,而 background 标签不支持。在某些情况下,尤其是。在可点击的图标上,使用 layout_margin 而不是 padding 是个坏主意,因为 padding 属于视图并且会使点击区域更大!。

原文由 Nishanth 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题