[英]Custom Title bar in Android
我创建了一个自定义标题栏,它包含一个TextView和标签。 我已将此添加到活动中,如下所示
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.homepage);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.windowtitle);
但现在我想在标题栏的textview中显示当前时间。
如何才能做到这一点?
请尝试下面的代码
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());
你可以得到像这样的文本视图。用textview
一个mytitle.xml
布局,然后添加它将起作用的代码..
@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 ");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.