[英]Scale background image in LinearLayout
我遇到以下問題;
我有一個帶有 ScrollView 的 LinearLayout,在 ScrollView 中有一些自定義視圖。 現在我想在 LinearLayout 的背景中放置一個圖像,並保持圖像的原始大小。 但是當我設置 LinearLayout 的背景屬性時,它會拉伸以適應全屏。 我怎樣才能使圖像保持其原始大小?
我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/some_drawable">
<ScrollView
android:id="@+id/scrollview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:fillViewport="true">
<some.custom.layout
android:id="@+id/mainLayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</ScrollView>
</LinearLayout>
Egor 答案的代碼示例:
<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/background"
/>
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</FrameLayout>
嘗試在內部使用帶有 ImageView 和 LinearLayout 的 FrameLayout。 例如,嘗試更改圖像的 alpha 並將其移動到 FrameLayout 中的前景,因此 LinearLayout 保持在背景上。 希望這可以幫助!
替代 ColdForged 的建議,您可以從LinearLayout
移動到RelativeLayout
並使用ImageView
來顯示圖像而不是更改背景。 與LinearLayout
不同, RelativeLayout
中的視圖可能會產生干擾。
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layoutLogin"
android:layout_alignLeft="@id/layoutLogin"
android:layout_alignRight="@id/layoutLogin"
android:layout_alignTop="@+id/layoutLogin"
android:adjustViewBounds="true"
android:background="@mipmap/ic_photo_bg"
android:scaleType="fitXY" />
<LinearLayout
android:id="@+id/layoutLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
你可以看看InsetDrawable 。 要么,要么可能需要您對布局進行子類化以按照您希望的方式繪制,因為背景元素沒有您需要的能力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.