在 Android 应用程序中水平和垂直重复线性布局的背景非常简单。您只需要使用 tileMode(参见 How to make android app's background image repeat)
但是 xml 文件中的 tileMode 不允许我仅在垂直轴上重复背景图像。有一个 tileModeY,但您无法从 xml 访问它。
我正在尝试将该属性修改为“重复”。
我有一个线性布局的 main.xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainMenuLayout"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
我在 drawable/welcomebackground.png 中有一个图像,在 drawable/welcomebackgroundpattern.xml 中还有一个图像的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/welcomebackground"
android:dither="true" />
在我的 Activity 中,我试过这个:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
BitmapDrawable background = (BitmapDrawable) res.getDrawable(R.drawable.welcomebackground);
background.setTileModeY(Shader.TileMode.REPEAT);
(new View(this)).setBackgroundResource(R.drawable.welcomebackground);
}
但我的背景仍然是全黑的。当然,我可以从 xml 添加它,但它在 x 轴和 y 轴上重复出现。
所以我的问题很简单:如何在我的 Activity 中垂直重复“welcomebackground”图像?
最佳答案
我找到了解决方案:Android Bitmap Tile on X Only
关于android - 如何向 Android 应用程序添加垂直平铺(仅)背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921925/