【问题标题】:Android. Layout background stretches安卓。布局背景延伸
【发布时间】:2012-07-12 09:05:08
【问题描述】:

我想在 RelativeLayout 中放置背景图像。背景图像中有一些物体,比如椅子、桌子、一些卡片等。

android:background="@drawable/remy_menu_background" 但是这该死的东西在屏幕上被拉长了,卡片和桌子看起来被拉长了。

我该怎么做才能让它在任何屏幕上看起来都不错?

【问题讨论】:

标签: android background screen


【解决方案1】:

您可以使用 XML drawable 使用标签来禁用拉伸:

<bitmap android:src="@drawable/background" android:gravity="center" />

【讨论】:

  • 嗯。我已经尝试过了,但它使应用程序崩溃。在“图形布局”选项卡中,菜单 xml 上显示“找不到以下类:-位图(修复构建路径,编辑 XML)”关于我做错了什么有什么想法吗?
  • 添加 android:scaleType="fitCenter" 或 "center"
  • @AndreiBogdan 这个 XML 文件进入 res/drawable,而不是 res/layout。
  • 图片小于屏幕时不起作用
【解决方案2】:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/main_backgroundImage"
    android:layout_width="match_parent" // comment: stretches picture in the width if too small. 
                                           use "wrap_content" does not stretch, but leaves space
    android:layout_height="match_parent" // in my case I always want the height filled
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop" // will crop picture left and right , so it fits in height and keeps aspect ratio
    android:contentDescription="@string/image"
    android:src="@drawable/your_image" />

<LinearLayout
    android:id="@+id/main_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>

【讨论】:

    【解决方案3】:

    您可以在这种情况下使用 .9.png 图像,并且可以指定图像的哪个区域应该拉伸,哪些不应该。您可以使用 android-sdk 内“tools”文件夹下的“draw9patch”工具将任何 png 转换为 .9.png。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2020-04-18
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多