android - 防止背景图像拉伸(stretch) View

标签 android

每当我为使用 wrap_content 布局的 View 分配背景时,如果背景图像大于 View ,则 View 会被拉伸(stretch)以容纳整个背景。为什么会这样以及如何预防?即使图像是 9-patch 并且标记了可拉伸(stretch)区域,它也会发生 - 为什么图像没有缩小到 View 的大小?

最佳答案

您可以使用 RelativeLayout 并在其中创建两个元素。背景的 ImageView 和内容的另一个 View 。使用 android:layout_alignBottom 到 ImageView 并将值设置为内容 View 的 id。还要设置 ImageView 的 scaleType 为 fitXY。

<RelativeLayout android:id="@+id/relativeLayout1" 
  android:layout_height="wrap_content" 
  android:layout_gravity="center" 
  android:layout_width="fill_parent">

<ImageView android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:scaleType="fitXY"
  android:src="@drawable/chronometer_bg"
  android:id="@+id/imageView_chronometerBackground"
  android:layout_alignBottom="@id/chronometer" />

<Chronometer android:text="Chronometer"
      android:id="@+id/chronometer" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true"
      android:textSize="32sp"
      android:gravity="center" />

</RelativeLayout>

关于android - 防止背景图像拉伸(stretch) View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4723500/

相关文章:

Android:在单后退键按下软键盘应该被隐藏并且 Activity 也应该完成,如何让它成为可能?

android - CoordinatorLayout 留空底部空间

android - 如何访问自定义资源目录下的资源文件?

android - 带有 html Table 的 TextView 与带有几个 TextView 的 TableLayout

android - 现有 Android 虚拟设备列表为空

java - Android 删除 View 崩溃

android - 如何在多个通知的状态栏中显示单个通知图标

android - 如何在 fragment 之间导航时消除白色闪烁/闪光?

android - 使用重力 : top 在 ImageView 中定位图像

android - 在已签名的应用程序中看不到 Google map