我必须创建一个自定义标题栏,以设置动态文本。这就像在“动作栏”上设置“标题”,然后在其下方设置动态字幕一样。
在这种情况下,默认情况下,App标题位于左侧。动态文本位于右侧,并且必须动态更改。
这是布局custom_titlebar.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="vertical" >
<TextView
android:id="@+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="left" />
<TextView
android:id="@+id/title_right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right"
android:layout_alignParentRight="true" />
</RelativeLayout>
在清单中,我定义了:
<application
android:theme="@style/Theme.NoTitle" >
并在活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//HAVE COMENTED THIS, BECAUSE IT THROWS: You cannot combine custom titles with other title feature..
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_titlebar);
TextView title = (TextView) findViewById(R.id.title_left_text);
title.setText(getTitle());
要动态更改文本,我还可以在Activity上进行操作:
public void setTitleStatus(String right) {
if (right.length() > 20) {
right = right.substring(0, 20);
}
TextView status = (TextView) findViewById(R.id.title_right_text);
status.setText(right);
}
问题是在我设置标题(title.setText(getTitle());
)的onCreate的行上抛出了NPE
改变
title.setText(getTitle().toString());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句