Padding in ImageView is not Working(ImageView 中的填充不起作用)
问题描述
我想制作一个包含顶栏和图像的屏幕.我使用 TextView 作为顶部栏,使用 ImageView 作为图像.我只想将填充设置为图像视图.我的 xml 在下面给出.填充操作不起作用.谁能解释一下为什么以及该怎么做?
I want to make a screen which contains a top bar and an image. I use TextView for the top bar and ImageView for the image. I want to set padding only to the image view. My xml is given below. The padding action is not working. Can anybody explain why and what to do?
<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 所以应该如下:
You should use layout_margin instead of padding so it should be as following:
<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,而不是使用
and you can specify 1 layout_margin for all directions so instead of using
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
你可以使用:
android:layout_margin="10dip"
希望这是您正在寻找的.否则给我反馈;)
hope this is what you are looking for. give me a feedback otherwise ;)
更新
您也可以设置 cropToPadding:p>
you can set cropToPadding also:
<ImageView
...
android:cropToPadding="true"
android:padding="15dp"
android:scaleType="centerInside"
...
这篇关于ImageView 中的填充不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ImageView 中的填充不起作用
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
