问题是唯一的Window http://developer.android.com/reference/android/view/Window.html执行 (PhoneWindow http://android.git.kernel.org/?p=platform/frameworks/policies/base.git;a=blob_plain;f=phone/com/android/internal/policy/impl/PhoneWindow.java;hb=HEAD)使用一个LayoutInflater http://developer.android.com/reference/android/view/LayoutInflater.html in its setFeatureInt
方法并实例化新布局inflate http://developer.android.com/reference/android/view/LayoutInflater.html and attachToRoot=true
。因此,当你打电话时setFeatureInt
,新的布局不是replaced but attached到内部标题容器,因此绘制在彼此之上。
您可以使用以下辅助方法来解决此问题,而不是setFeatureInt
。在设置新的自定义标题功能之前,助手只是从内部标题容器中删除所有视图:
private void setCustomTitleFeatureInt(int value) {
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);
// remove all views from titleContainer
((ViewGroup) getWindow().findViewById(titleContainerId)).removeAllViews();
// add new custom title view
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, value);
} catch(Exception ex) {
// whatever you want to do here..
}
}
我不确定当前是否setFeatureInt
行为是有意为之的,但它肯定没有以某种方式记录下来,这就是为什么我会将其带给 Android 开发人员;)
EDIT
正如评论中指出的,上述解决方法并不理想。而不是依赖于com.android.internal.R.id.title_container
常数你可以简单地hide每当您设置新标题时,都会使用旧的自定义标题。
假设您有两个自定义标题布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/custom_title_1" ...
and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/custom_title_2" ...
你想更换custom_title_1
with custom_title_2
,你可以隐藏前者并使用setFeatureInt
添加后者:
findViewById(R.id.custom_title_1).setVisibility(View.GONE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_2);