【问题标题】:Scale background image in LinearLayout在 LinearLayout 中缩放背景图像
【发布时间】:2018-09-11 00:40:07
【问题描述】:

我遇到以下问题;

我有一个带有 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>

【问题讨论】:

  • 看到一些人时不时地支持这个问题(谢谢!),但想在这里声明一下,这个问题现在已经有 9 年了,而且在 Android 世界中发生了很多事情。现在很可能有更好的方法来解决这个问题!

标签: android layout


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    尝试使用带有 ImageView 和 LinearLayout 的 FrameLayout。例如,尝试更改图像的 alpha 并将其移动到 FrameLayout 中的前景,因此 LinearLayout 保持在背景上。希望这会有所帮助!

    【讨论】:

    【解决方案3】:
    <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>
    

    【讨论】:

      【解决方案4】:

      除了 ColdForged 的​​建议之外,您可以从 LinearLayout 移动到 RelativeLayout 并使用 ImageView 来显示图像,而不是更改其背景。 RelativeLayout 中的视图可能会与LinearLayout 不同。

      【讨论】:

      • 我已经尝试过了,但是如何在图像视图上绘制 ScrollView 的内容,以便图像成为背景?
      • 也许通过为滚动视图背景设置 alpha
      【解决方案5】:

      您可以查看InsetDrawable。要么这样,要么可能需要您对布局进行子类化以按照您希望的方式绘制,因为背景元素没有您需要的能力。

      【讨论】:

        猜你喜欢
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2017-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多