[英]Relative layout wrap width and height according to background image
我面臨的問題是非常奇怪的相對布局沒有根據image.width和height彎曲。它可以在全屏上顯示relativelayout,如您所見,可以看到Image Code:
<?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>
當我將ImageView放置在相對布局中時,我解決了這個問題,如您所見Image,它可以完美工作。
碼:
<?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>
但是問題是我不想使用imageview,因為它仍然以全屏顯示RelativeLayout。 我想根據圖像包裝相對布局,就像上面的代碼所示。
// 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>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A0000000"
android:clickable="true"
android:focusable="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- The Background Image -->
<ImageView
android:layout_width="wrap_content"
android:scaleType="fitXY"
android:layout_centerInParent="true"
android:id="@+id/background_image"
android:layout_height="wrap_content"
android:src="@drawable/bg_pic"
/>
<!-- The view group containd around the image -->
<LinearLayout
android:orientation="vertical"
android:layout_alignBottom="@id/background_image"
android:layout_alignLeft="@id/background_image"
android:layout_alignRight="@id/background_image"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
這應該可以解決您的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.