我想使用一个小的(图标大小)图像作为我的 View 的背景,但它被拉伸(stretch)以填充所有 View 。
任何解决方案(包括在我当前的 View 下放置一个 View 并使后者透明的某种方式)将不胜感激。
谢谢!
当前代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/icon"
android:id="@+id/layout_main"
>
最佳答案
你有两个选择:
将图像放大。具体来说,在其边缘周围添加一条黑线,然后使用 tools/draw9patch 工具使该黑线成为“可拉伸(stretch)”部分。当图像放大到背景时,您的原始图标将不会被拉伸(stretch)。
放弃 android:background 并改用顶级 RelativeLayout。这应该有效
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageSwitcher android:id="@+id/ImageSwitcher01" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_height="wrap_content" android:background="@drawable/icon"> </ImageSwitcher> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true"> <TextView android:text="Your old top level here" android:layout_height="wrap_content" android:layout_width="wrap_content"> </TextView> </LinearLayout> </RelativeLayout>
希望对你有帮助
关于Android:如何在 View 中设置居中背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3030194/