[英]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.