簡體   English   中英

如何使android actionbar背景圖像重復x和y

[英]How to make android actionbar background image repeat x and y

如何使以下樣式為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文件,該文件表示要重復的圖像。

仍在可繪制文件夾下,創建一個包含以下內容的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>

我注意到,根據您使用的API級別,在重新設置Actionbar時Android會出現一些問題。 在這種情況下,使用更新的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);

希望這對您和其他人有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM