每当我为使用 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/