Android中的自定义标题栏

时间:2012-10-10 08:39:13

标签: android titlebar

我创建了一个自定义标题栏,它包含一个TextView和标签。我已将此添加到活动中,如下所示

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.homepage);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.windowtitle);

但现在我想在标题栏的textview中显示当前时间。

如何做到这一点?

2 个答案:

答案 0 :(得分:1)

请尝试以下代码

LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.windowtitle, null, true);
TextView textView = (TextView) view.findViewById(R.id.txtdate);
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
txtdate.setText(c.getTime());

答案 1 :(得分:1)

你可以像这样获得文本视图。用mytitle.xml创建一个textview布局,然后添加它将起作用的代码..

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.main);

  if ( customTitleSupported ) {
      getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
  }

  final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
  if ( myTitleText != null ) {         
      myTitleText.setText(" TITLE  ");     
  }
}