如何使用XML在Android上使我的图像更大?

时间:2015-08-27 18:22:33

标签: android xml android-layout

我的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_widthandroid:layout_height属性,但是如果我用一个数字(例如,200dp width100dp height)添加ImageView,那么override func viewDidLoad() { super.viewDidLoad() splitViewController?.delegate = self let rect: CGRect = UIScreen.mainScreen().bounds if rect.height > rect.width { // am in portrait: trick to force the master view to open self.splitViewController?.preferredDisplayMode = .PrimaryOverlay } 就是屏幕左侧而不是中心。

我希望将图像居中,使其比原始图像大一点。在屏幕中央显示的同时,我找不到更大的东西。

有可能吗?我怎么能这样做?

提前致谢!

4 个答案:

答案 0 :(得分:2)

使用scaletype并在图片视图部分添加android:adjustviewbounds="true"

答案 1 :(得分:1)

将您想要的尺寸保持为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>

修改

如果这是您希望布局看起来像

enter image description here

然后您需要做的就是添加

android:layout_gravity="center_horizontal"

ImageView,并在width

中指定heightdp

答案 2 :(得分:0)

您可以为imageview添加3个属性:

android:scaleType=""
android:scaleX=""
android:scaleY=""

可以看到ScaleType类型here。基本上,你会想要使用CENTER_CROP。

scaleX和scaleY使用float作为参数,定义两个轴的比例。请参阅参考here

答案 3 :(得分:0)

上面的两个代码都有效(Andrew BrookeIntelliJ Amiya,谢谢!)但现在我找到了一个“技巧”来避免这两种方法。要使用ImageViewandroid:minWidth创建android:minHeight,以便最终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放在您想要的尺寸中心。