如何在屏幕布局内设置图像?

how to set images inside a layout on screen?

我需要将两张图片一张放在屏幕中央,一张放在另一张下面。他们需要在中心。图像将根据代码不断变化,那么我如何让图像在所有屏幕上工作而不移动或尺寸增加或减小?

我试过使用网格视图,但两张图片的代码似乎太多了。它有效,但还有更有效的方法吗?

如果你总是只有 2 张图片,你可以在 xml 文件中这样设置它:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true" />

</RelativeLayout> 

第一张图片在父图片中居中,第二张图片水平居中并与屏幕底部对齐。

祝你好运。

对于这种类型的设计,我总是在屏幕上创建一个中心点,检查下面的代码 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/center_point" />

<View 
    android:id="@+id/center_point"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_centerInParent="true"/>

<ImageView
    android:id="@+id/image2"
    android:src="@drawable/ic_launcher"
    android:layout_below="@+id/center_point"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

肯定有用。

祝一切顺利

创建一个包含您的两个 ImageView 的垂直 Linear-layout,然后为它们设置一个相等的 layout_weight。为您的 Linear_layout

设置 layout_centerInParent true
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/lock_layout_keypad"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:layout_marginBottom="4dp">


    <ImageView
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</LinearLayout>

您可以删除 android:layout_weight

这里的代码可能会有所帮助 you.You 必须创建一个 LinearLayout 并设置他的垂直方向和 layout_gravity = center horizontal.After 创建两个自动位于中心位置的图像视图下面的第一个屏幕 other.I 希望这将有助于 you.Happy 编码 :)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal">


    <ImageView
        android:id="@+id/image_view_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />

    <ImageView
        android:id="@+id/image_view_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>