如何使以下样式重复 android 操作栏的图像 x 和 y。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background" >@drawable/action_bar_bg</item>
</style>
最佳答案
在 drawable 文件夹下,添加您的 my_background.png 文件,该文件表示要重复的图像。
仍然在 drawable 文件夹下,创建一个 action_bar_bg.xml 包含:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/my_background"
android:tileMode="repeat" />
注意 android:tileMode="repeat"
语句。这将指示 android 重复图像。
打开您的 styles.xml 或您用来设置样式的任何文件,并根据上面的位图配置您的背景图片:
<style name="ActionBarStyle" parent="android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/action_bar_bg</item>
</style>
我注意到 Android 在根据您使用的 API 级别重新显示 Actionbar 时会出现一些问题。在这种情况下,使用更新的 API 可以更好地避免麻烦和定制/黑客攻击。因此,您需要仔细检查您使用的目标 API、样式、可绘制对象、布局、颜色和主题,以确定它们是否都已链接和设置。
一旦声明了位图,就可以很容易地以编程方式设置可重复的背景:
//highlight visited item using image declared in 'bitmap' tag
if (Build.VERSION.SDK_INT >= 16)
view.setBackground(
getResources().getDrawable(R.drawable.action_bar_bg)
);
else
view.setBackgroundDrawable(
getResources().getDrawable(R.drawable.another_action_bar_bg)
);
如果您想探索更多的背景编码,我建议您尝试使用 BitmapDrawable.setTileModeXY
方法
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.action_bar_bg);
BitmapDrawable bdw = new BitmapDrawable(bmp);
bdw.setTileModeXY(Shader.TileMode.REPEAT,
Shader.TileMode.REPEAT);
yourView.setBackgroundDrawable(bdw);
希望对您和其他人有所帮助!
关于android - 如何使android actionbar背景图像重复x和y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961296/