Android - 在运行时更改自定义标题视图 [英] Android - change custom title view at run time

查看:33
本文介绍了Android - 在运行时更改自定义标题视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中为每个活动使用自定义标题视图.在其中一项活动中,基于单击按钮,我需要更改自定义标题视图.现在每次我调用 setFeatureInt 时都可以正常工作.

但如果我尝试更新自定义标题中的任何项目(比如更改按钮的文本或标题上的文本视图),则不会进行更新.

通过代码调试显示文本视图和按钮实例不为空,我也可以看到自定义标题栏.但是文本视图或按钮上的文本没有更新.有没有其他人遇到过这个问题?我该如何解决?

谢谢.

编辑

这是我尝试过的.即使调用 postInvalidate 也不会更新.

 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.text_title);TextView databar = (TextView) findViewById(R.id.title_text);databar.setText("一些文本");databar.postInvalidate();按钮 leftButton = (Button) findViewById(R.id.left_btn);leftButton.setOnClickListener(mLLeftListener);leftButton.setText("左Btn");leftButton.postInvalidate();按钮 rightBtn = (Button) findViewById(R.id.right_btn);rightBtn.setOnClickListener(mRightListener);rightBtn.postInvalidate();

解决方案

问题是只有 Window 实现(PhoneWindow) 使用 LayoutInflater 在其 setFeatureInt 方法和使用 inflate 和 <代码>attachToRoot=true.因此,当您调用 setFeatureInt 时,新布局不会被替换,而是附加到内部标题容器,从而在彼此之上绘制.

您可以通过使用以下辅助方法而不是 setFeatureInt 来解决此问题.在设置新的自定义标题功能之前,助手只是从内部标题容器中删除所有视图:

<代码>私有无效 setCustomTitleFeatureInt(int 值) {尝试 {//检索 com.android.internal.R.id.title_container(=0x1020149) 的值int titleContainerId = (Integer) Class.forName("com.android.internal.R$id").getField("title_container").get(null);//从 titleContainer 中删除所有视图((ViewGroup) getWindow().findViewById(titleContainerId)).removeAllViews();//添加新的自定义标题视图getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, value);} 捕捉(异常前){//无论你想在这里做什么..}}

我不确定当前的 setFeatureInt 行为是否是有意为之,但肯定没有以一种或另一种方式记录在案,这就是我将其带到 android 开发人员的原因;)

编辑

正如评论中所指出的,上述解决方法并不理想.无需依赖 com.android.internal.R.id.title_container 常量,您可以在设置新标题时简单地隐藏旧的自定义标题.

假设您有两个自定义标题布局:

如果你想用 custom_title_2 替换 custom_title_1,你可以隐藏前者并使用 setFeatureInt 添加后者:

findViewById(R.id.custom_title_1).setVisibility(View.GONE);getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_2);

I am using a custom title view in my application for each activity. In one of the activities, based on button clicks I need to change the custom title view. Now this works fine every time when I make a call to setFeatureInt.

But if I try to update any items in the custom title (say change the text of a button or a text view on the title), the update does not take place.

Debugging through the code shows that the text view and button instances are not null and I can also see the custom title bar. But the text on the text view or the button is not updated. Has anyone else faced this problem? How do I resolve it?

Thanks.

EDIT

Here's what I tried. Does not get updated even on calling postInvalidate.

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.text_title);

    TextView databar = (TextView) findViewById(R.id.title_text);
    databar.setText("Some Text");
    databar.postInvalidate();

    Button leftButton = (Button) findViewById(R.id.left_btn);
    leftButton.setOnClickListener(mLeftListener);
    leftButton.setText("Left Btn");
    leftButton.postInvalidate();

    Button rightBtn = (Button) findViewById(R.id.right_btn);
    rightBtn.setOnClickListener(mRightListener);
    rightBtn.postInvalidate();

解决方案

The problem is that the only Window implementation (PhoneWindow) uses a LayoutInflater in its setFeatureInt method and instantiates the new layout with inflate and attachToRoot=true. Consequently, when you call setFeatureInt, the new layouts are not replaced but attached to the internal title container and thus drawn on top of each other.

You can workaround this by using the following helper method instead of setFeatureInt. The helper simply removes all views from the internal title container before the new custom title feature is set:


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..
    }
}

I'm not sure whether the current setFeatureInt behaviour is intended, but it is certainly not documented one way or the other which is why I'll take this to the android devs ;)

EDIT

As pointed out in the comments, the aforementioned workaround is not ideal. Instead of relying on the com.android.internal.R.id.title_container constant you could simply hide the old custom title whenever you set a new one.

Let's assume you have two custom title layouts:

<?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" ...

and you want to replace custom_title_1 with custom_title_2, you could hide former and use setFeatureInt to add the latter:

findViewById(R.id.custom_title_1).setVisibility(View.GONE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_2);

这篇关于Android - 在运行时更改自定义标题视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆