2016-09-27 69 views
-1

我想爲我的項目製作一個日曆應用程序,並且我想以標題「月」和副標題「日期和年」顯示當前日期。在Android的操作欄中顯示當前日期

我已經擴展了操作欄高度並隱藏了操作欄標題,但是如何將當前日期添加到操作欄的起始點?

回答

0

添加的TextView到動作條:在DD

Dynamic TextView on ActionBar

今天的日期/ MM/YYYY格式:

Calendar c = Calendar.getInstance(); 
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
String date = dateFormat.format(c.getDate()); 

//if you want dd/MM/yyyy as separate units 
String[] separatedDate = date.split("/"); 
0

手動添加一個TextView操作欄:

TextView tv = new TextView(this); 
     tv.setText(<Replace with DateTime Related Variable here>); 
     tv.setTextColor(getResources().getColor(R.color.WHITE)); 
     tv.setOnClickListener(this); 
     tv.setPadding(5, 0, 5, 0); 
     tv.setTypeface(null, Typeface.BOLD); 
     tv.setTextSize(14); 
     menu.add(0, FILTER_ID, 1, R.string.matchmacking).setActionView(tv).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 

你很好走。

這是改編自Yashdeep Patel

0

獲取當前日期

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"),Locale.getDefault()); 
      SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
      String currentDate = df.format(c.getTime()); 

,並可以設置工具欄標題或標題的ActionBar

toolbar.setTitle(currentDate); 

getActionBar().setTitle(currentDate);