[英]How can I make my image bigger on Android with XML?
我的ImageView
上有一个图像,但我不知道如何使用 XML使它变大。 我的图像如下:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="@drawable/image"/>
我尝试更改android:layout_width
和android:layout_height
属性,但如果我将它们与一个数字(例如, 200dp width
和100dp height
)放在一起,则ImageView
位于屏幕左侧而不是中心。
我想将图像居中并使其比原始图像大一点。 我找不到任何可以让它在屏幕中央显示的同时变大的东西。
是否可以? 我怎样才能做到这一点?
提前致谢!
使用scaletype
并在图像视图部分添加android:adjustviewbounds="true"
。
保持你想要的大小作为ImageView
的宽度和高度,并将其父级的gravity
更改为center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/img"
android:layout_marginTop="40dp"
android:src="@drawable/image"/>
</LinearLayout>
编辑:
如果这是您希望布局的样子
那么你需要做的就是添加
android:layout_gravity="center_horizontal"
到您的ImageView
,并在dp
指定width
和height
上面的两个代码都有效( Andrew Brooke
和IntelliJ Amiya
,谢谢!)但现在我找到了一个“技巧”来避免这两种方法。 这是使用android:minWidth
和android:minHeight
创建ImageView
所以最终的ImageView
将是这样的:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:minWidth="100dp"
android:minHeight="70dp"
android:src="@drawable/image"/>
这样,您就可以将您的ImageView
以您想要的大小居中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.