我的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
,那么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
}
就是屏幕左侧而不是中心。
我希望将图像居中,使其比原始图像大一点。在屏幕中央显示的同时,我找不到更大的东西。
有可能吗?我怎么能这样做?
提前致谢!
答案 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>
修改强>
如果这是您希望布局看起来像
然后您需要做的就是添加
android:layout_gravity="center_horizontal"
到ImageView
,并在width
height
和dp
答案 2 :(得分:0)
您可以为imageview添加3个属性:
android:scaleType=""
android:scaleX=""
android:scaleY=""
可以看到ScaleType类型here。基本上,你会想要使用CENTER_CROP。
scaleX和scaleY使用float作为参数,定义两个轴的比例。请参阅参考here。
答案 3 :(得分:0)
上面的两个代码都有效(Andrew Brooke
和IntelliJ Amiya
,谢谢!)但现在我找到了一个“技巧”来避免这两种方法。要使用ImageView
和android: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
放在您想要的尺寸中心。