Android的 - 在运行时更改自定义标题视图自定义、视图、标题、Android

2023-09-12 02:36:50 作者:烟花再美不过瞬间

我用我的应用程序的每个活动的自定义标题视图。在活动之一,基于按钮点击我需要更改自定义标题视图。现在这工作正常,每次当我打电话到setFeatureInt。

但是,如果我尝试更新的自定义标题的任何项目(比如改变一个按钮或标题文本视图的文本),更新就不会发生。

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

感谢。

修改

下面是我的尝试。没有得到关于调用postInvalidate甚至更新。

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

TextView中的DataBar =(TextView中)findViewById(R.id.title_text);
databar.setText(一些文本);
databar.postInvalidate();

按钮leftButton =(按钮)findViewById(R.id.left_btn);
leftButton.setOnClickListener(mLeftListener);
leftButton.setText(左BTN。);
leftButton.postInvalidate();

按钮rightBtn =(按钮)findViewById(R.id.right_btn);
rightBtn.setOnClickListener(mRightListener);
rightBtn.postInvalidate();
 
android的标题栏如何修改

解决方案

现在的问题是,只有 窗口 实现(PhoneWindow)使用LayoutInflater在其 setFeatureInt 方法和实例化与inflate和 attachToRoot = TRUE ,因此,当你调用 setFeatureInt ,新的布局都没有的替换但附应用于内标题容器,因此绘制在彼此的顶部。

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

 
私人无效setCustomTitleFeatureInt(int值){
    尝试 {
    //获取价值com.android.internal.R.id.title_container(= 0x1020149)
    INT titleContainerId =(整数)的Class.forName(
    com.android.internal.R的$ id)getfield命令(title_container)获得(空)。

    //删除titleContainer所有视图
    ((ViewGroup中)getWindow()findViewById(titleContainerId)。)removeAllViews()。

    //添加新的自定义标题视图
    。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,价值);

    }赶上(例外前){
    //任何你想做的事情在这里..
    }
}
 

我不知道目前的 setFeatureInt 行为是否意,但它肯定是不记录这就是为什么我要拿这个来的一种方式或其他Android的开发者;)

修改

正如在评论中,上述解决办法不理想。而不是依赖 com.android.internal.R.id.title_container 不变的,你可以简单的隐藏的旧的自定义标题,只要你设置一个新的之一。

让我们假设你有两个自定义标题布局:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的机器人:ID =@ + ID / custom_title_1......
 

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的机器人:ID =@ + ID / custom_title_2......
 

和要替换 custom_title_1 custom_title_2 ,你可以隐藏前,并使用 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);