在LinearLayout中缩放背景图像

Jor*_*ris 11 layout android

我有以下问题;

我有一个LinearLayout,里面有一个ScrollView,在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>
Run Code Online (Sandbox Code Playgroud)

Asa*_*ssi 25

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>
Run Code Online (Sandbox Code Playgroud)


Ego*_*gor 20

尝试将FrameLayout与ImageView和LinearLayout一起使用.例如,尝试更改图像的alpha并将其移动到FrameLayout中的前景,因此LinearLayout保留在背景上.希望这可以帮助!

  • 这很好用,但我认为更优雅的解决方案可以在这里找到:http://stackoverflow.com/questions/2781593/background-image-placement (2认同)