android - ImageView 中的填充不起作用

标签 android android-layout imageview padding

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

关于android - ImageView 中的填充不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172350/

相关文章:

java - 如何从 Android 应用程序中的异步任务返回位图

android - 更改背景颜色时 EditText 边距丢失

android - 如何使用内置图标和模板?

android - 小部件中带有 fragment 的选项卡式对话框

android - 在 RecyclerView GridLayoutManager 中将最大行数 4 和列设置为动态

android - 在 Touch Android 上具有拖动、滚动和缩放功能的三角形 ImageView

android - 无法将 ImageBitmap 从 Canvas 位图设置到 ImageView

android - 未找到 UID 10208 [安全异常] 的持久权限授予

android - 如何创建不重复的组合并存储在android studio的数据库中?

android - 如何以编程方式获取根布局并将其设置为内容 View