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

时间:2013-01-23 21:05:33

标签: android drawable

嗯,我想我遇到了一些错误。

LayerLists中的位图似乎存在错误:http://www.michaelpardo.com/2011/10/repeating-bitmaps-inside-layerlists/

我有一个Layer-List,我想在我的自定义标题栏中用作Background-Drawable。

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

修改 要使用链接的解决方法,我需要以编程方式设置标题栏的背景,但我不知道如何。这似乎不起作用,它改变了整个屏幕的背景(包括实际的内容视图):

LayerDrawable layer = (LayerDrawable)getResources().getDrawable(R.drawable.custom_titlebar_background);
        setLayerDrawableBitmapsRepeating(layer);
        getWindow().setBackgroundDrawable(layer);

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

2 个答案:

答案 0 :(得分:1)

像这样创建自定义标题栏:

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/themes.xml -

创建一个继承默认主题的主题,并将背景样式设置为我们自己的样式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTheme" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>   
    </style> 
</resources>

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>

AndroidMANIFEST.xml中,在应用程序(针对整个应用程序)或活动(仅针对此活动)标记中设置主题属性

<activity android:name=".CustomTitleBar" android:theme="@style/customTheme" ...

来自Activity(名为CustomTitleBar):

@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);

}

答案 1 :(得分:0)

我找到了解决方案。它完全是脏的,并使用无证件的成员。这至少对我有用,因为我的自定义标题栏仅用于2.x设备以获得类似Holo的外观。

似乎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版本上的tileable位图。