【问题标题】:How to set the ScaleType on a LinearLayout's background image如何在 LinearLayout 的背景图像上设置 ScaleType
【发布时间】:2015-10-12 14:08:53
【问题描述】:

我有这样的线性布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@drawable/bgapp" >

我现在需要的是设置背景图像ScaleType 值以便每次都填满屏幕的可能性,这是因为我要下载图像然后将其设置为背景但没有ScaleType 图像不会根据屏幕密度保持其纵横比。

我发现的是这个解决方案:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/bgapp"
    android:scaleType="centerCrop"
    android:gravity="bottom|left" />

但它没有按预期工作.. 有没有办法设置 LinearLayout scaleType 属性或者我需要更改我的布局?

【问题讨论】:

  • 你不能在线性布局中使用 scaletype .. 你可以检查这个stackoverflow.com/a/16139059/931982
  • @pskink 你能解释一下如何使用它吗?
  • @pskink 我明天试试,我会告诉你的,谢谢:)

标签: android


【解决方案1】:

视图的背景会根据View 的大小进行拉伸。

ImageView 支持图像缩放,要与LinearLayout 一起使用,您需要一个额外的容器,它将覆盖 2 个孩子(例如 FrameLayout):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/bgapp"
    android:scaleType="centerCrop"/>

  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    ...
  </LinearLayout>
</FrameLayout>

【讨论】:

  • @pskink 是的,您可能对自定义可绘制对象是正确的。我说的是View 背景属性一般是如何工作的。
  • 是的,我也看到了这种类型的解决方案,但我不想仅仅为背景图像更改我的 xml 结构,我仍然希望有一个允许 LinearLayout 具有 scaleType 选项的解决方案。无论如何,谢谢!
  • 你有什么解决办法吗? @Signo
  • @PrashanthDebbadwar 哈哈,我现在只是在寻找一个 :D 仍然没有对不起 :( 顺便说一句,这是一个正确的解决方案,我只是不想更改我的 xml
  • 没用。我需要使用ScalingFrameLayout 来实现我的目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 2017-01-27
  • 2013-08-23
  • 1970-01-01
相关资源
最近更新 更多