我正在尝试使用布局为图像视图添加边框以将其包装并将边框设置为该特定布局的背景。但是,图像视图看起来很小,位于框架背景的左上角。
这是我的代码:
<RelativeLayout
android:id="@+id/Relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="40dp"
android:background="@drawable/pic_frame_big_01" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
如何修改此代码,使图像适合相对布局并占用它。
编辑更改了ImageView的代码,图像现在居中,但它显示为以父级为中心的非常小的图像。
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
答案 0 :(得分:0)
因为您将其设置为左上角对齐。删除它们并更改宽度和高度以匹配。试试这个:
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
修改:您的相对布局也在左上角对齐。尝试删除对齐属性。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:background="@drawable/border_red" >
<ImageView
android:src="@drawable/holotaglogo"
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
答案 1 :(得分:0)
尝试将android:padding="10dp"
添加到RelativeLayout
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="40dp"
android:background="@drawable/pic_frame_big_01"
android:padding="10dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/image1" />
</RelativeLayout>
android:src="@drawable/image1
也很重要..用项目中的某些图片替换image1
。