Android:设置自定义标题栏的可绘制背景

Android:设置自定义标题栏的可绘制背景,android,drawable,Android,Drawable,我想我遇到了一些虫子 层列表中的位图似乎有一个bug: 我有一个图层列表,我想在我的自定义标题栏中用作背景绘制 <style name="custom_titlebar_background"> <item name="android:background">@drawable/custom_titlebar_background</item> </style> 是否有任何方法可以获取对自定义标题栏的引用以调用setBackgroundDrawa

我想我遇到了一些虫子

层列表中的位图似乎有一个bug:

我有一个图层列表,我想在我的自定义标题栏中用作背景绘制

<style name="custom_titlebar_background">
<item name="android:background">@drawable/custom_titlebar_background</item>
</style>

是否有任何方法可以获取对自定义标题栏的引用以调用setBackgroundDrawable()或任何其他方法来设置自定义标题栏的背景?

创建自定义标题栏,如下所示:

res/layout/mytitle.xml
-这是表示标题栏的视图

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/myTitle"
  android:text="This is my new title"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
/>
res/values/styles.xml
-这是我们设置主题以使用标题背景所需的背景的地方

<?xml version="1.0" encoding="utf-8"?>
<resources> 
   <style name="WindowTitleBackground">     
        <item name="android:background">@drawable/custom_titlebar_background</item>                   
    </style>
</resources>
从活动(称为CustomTitleBar)中:


我找到了解决办法。它是完全肮脏的,并且使用未注册的成员。这至少对我来说是可行的,因为我的自定义标题栏只在2.x设备上使用,以获得全息般的外观

android标题视图似乎总是具有相同的id

// http://stackoverflow.com/questions/820398/android-change-custom-title-view-at-run-time
        LayerDrawable layer = (LayerDrawable)getResources().getDrawable(R.drawable.custom_titlebar_background);
        setLayerDrawableBitmapsRepeating(layer);

        // get title view and set new background view
        try
        {
            // retrieve value for com.android.internal.R.id.title_container(=0x1020149)
            int titleContainerId = (Integer) Class.forName("com.android.internal.R$id").getField("title_container").get(null);
            View titleView = findViewById(titleContainerId);

            if(titleView != null)
            {
                titleView.setBackgroundDrawable(layer);
            }
        }
        catch(Exception e)
        {
            // nothing, just skip
        }

只要你没有做重要的事情,并且你的应用程序在没有它的情况下仍然可以工作,我认为没有理由不使用此解决方案来修复旧Android版本上的可平铺位图。

我已经编辑了我的问题。我不是在寻找如何制作自定义标题栏。我需要应用一个变通方法,因为Android有一个bug。
<?xml version="1.0" encoding="utf-8"?>
<resources> 
   <style name="WindowTitleBackground">     
        <item name="android:background">@drawable/custom_titlebar_background</item>                   
    </style>
</resources>
<activity android:name=".CustomTitleBar" android:theme="@style/customTheme" ...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

}
// http://stackoverflow.com/questions/820398/android-change-custom-title-view-at-run-time
        LayerDrawable layer = (LayerDrawable)getResources().getDrawable(R.drawable.custom_titlebar_background);
        setLayerDrawableBitmapsRepeating(layer);

        // get title view and set new background view
        try
        {
            // retrieve value for com.android.internal.R.id.title_container(=0x1020149)
            int titleContainerId = (Integer) Class.forName("com.android.internal.R$id").getField("title_container").get(null);
            View titleView = findViewById(titleContainerId);

            if(titleView != null)
            {
                titleView.setBackgroundDrawable(layer);
            }
        }
        catch(Exception e)
        {
            // nothing, just skip
        }