0

我只是想在全屏应用程序中将 ImageView 居中。我在这个网站上阅读了几篇关于人们问什么似乎是同一个问题的帖子,我已经尝试了我读过的所有内容,但我似乎无法将其置于中心位置。

这是我的布局代码。由于尝试了其他各种帖子,我意识到这里可能有一些矫枉过正的方法来获得居中。此代码将图像放在屏幕的左上角。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/RelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:layout_gravity="center"
    android:background="#000000"
    android:orientation="vertical" >

    <ImageView
       android:id="@+id/image"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_alignParentTop="false"
       android:layout_centerInParent="true"
       android:layout_gravity="center"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:adjustViewBounds="true"
       android:background="@android:color/black"
       android:contentDescription="@string/descImage"
       android:cropToPadding="false"
       android:gravity="center"
       android:src="@drawable/placeholder" />

这是我在 AndroidManifest.xml 中的全屏代码。

android:screenOrientation="landscape"
android:theme="@style/Theme.FullScreen">

Theme.FullScreen 定义为

<style name="Theme.FullScreen" parent="@android:style/Theme.Holo">
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowFullscreen">true</item>
</style>

谢谢。

4

5 回答 5

1

尝试这个。它可以在我的带有 4.2 的 Galaxy nexus 上找到(我只是将图像视图的宽度和高度更改为 wrap_content 并删除了不必要的和无意义的属性。)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/RelativeLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#000000">

    <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:background="@android:color/black"
            android:src="@drawable/ic_launcher" />
</RelativeLayout>

和主题...

<style name="Theme.FullScreen" parent="android:Theme.Holo.NoActionBar.Fullscreen"/>
于 2013-08-22T03:53:11.523 回答
0

That seems very excessive for what you're trying to do. Try this?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/descImage"
        android:src="@drawable/placeholder" />

</FrameLayout>
于 2013-08-22T03:43:19.640 回答
0

如果使用fill_parent大小,整个视图将不会以父级为中心,因为它是父级的大小。

如果将大小更改为wrap_content,视图的边界框将更改,并且将在父级内部居中。

您还可以使用该android:scaleType属性来imageView指定图像资源本身应如何在视图内布局。

默认scaleType会导致资源图像出现在 ImageView 的左上角,如果边界框大于实际图像本身。

您还可以使用该android:adjustViewBounds属性让操作系统根据图像资源自动为您调整边界框的大小。

于 2013-08-22T03:47:30.877 回答
0

在 ImageView 上使用 scaleType="fitCenter"。

于 2014-05-22T20:18:41.460 回答
0

fill_parent在更高版本的 Android 中已被替换,match_parent并且我使用了 SVG 可绘制资源,因此我不得不稍微修改上面的答案。

这对我有用:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.MyActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/my_drawable" />
</android.widget.RelativeLayout>
于 2017-11-15T10:01:04.953 回答