根据背景图像的相对布局包装宽度和高度

Fah*_*ood 1 android relativelayout android-layout

我面临的问题是非常奇怪的相对布局不是根据图像扭曲宽度和高度.它在全屏显示相对布局,因为你可以看到图像 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img_bg_wood">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/nine_man_morris" >
</RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我解决这个问题,当我把在相对布局的ImageView它完美的作品,你可以看到图像.

码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img_bg_wood">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/nine_man_morris" />

</RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

但问题是我不想使用imageview,因为它仍然在全屏显示RelativeLayout.我想根据图像包装relativelayout,就像上面的代码所示.

Har*_*ana 5

// try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img_bg_wood">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/nine_man_morris"
        android:gravity="center">

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

    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)