2013-07-24 64 views
0

以下是我的應用程序標題,我想在「DD/MM」位置設置當前日期時間。我怎樣才能以編程方式做到這一點?我使用actionBarSherlock如何以編程方式在actionBar Sherlock中設置文本

enter image description here

這是我如何設定的背景:

<style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar"> 
     <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item> 
     <item name="android:actionBarStyle">@style/Widget.MyTheme.ActionBar</item> 
    </style> 

    <style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar"> 
     <item name="android:background">@drawable/abs_bg</item> 
     <item name="background">#ffffff</item> 
</style> 
+0

你能提供你如何建立標頭中的代碼示例? – jimmithy

+0

標題是一個圖像,還沒有textview。我添加了代碼。查看問題的編輯 –

回答

0

如果您不需要使用任何選項菜單或任何其他actionBarSherlock功能,則不需要此庫。您可以使用任何主題與NoTitleBar並創建自定義RelativeLayout t創建標題。

styles.xml

<style name="AppTheme" parent="android:Theme.Light.NoTitleBar"> 
<!--Anything --> 
</style> 

你layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <RelativeLayout android:id="@+id/header" 
     android:layout_width="match_parent" 
     android:layout_height="50dip" 
     android:background="#6ccff6"> 

     <ImageView 
      android:id="@+id/logo" 
      android:adjustViewBounds="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/abs_bg" /> 

     <TextView 
      android:id="@+id/date" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="10dp" 
      android:textColor="@android:color/white" 
      android:text="July 25" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

    </RelativeLayout> 
    <!-- Put any other layout/view here --> 

</LinearLayout> 
2

我可能是誤會,但你可以只添加一個MenuItem它什麼也不做,設置爲文本(儘管這可能是一個不好的方法)。

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 
    String currentTime; 
    // Calculate your string 
    MenuItem item = menu.add(Menu.NONE, 0, Menu.NONE, currentTime); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case 0: 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

我認爲這將是一種方式來做到這一點,雖然我不知道它看起來有多好。

編輯:通過設置自定義背景,您可以在按下MenuItem時擺脫高亮顯示的ActionBar的藍色部分。下面是一個快速的方法來做到這一點(假設你只有那個菜單項):

<style name="BlankMenu" parent="@style/AppTheme"> 
    <item name="actionButtonStyle">@style/CustomItem</item> 
</style> 

<style name="CustomItem" parent="@style/Widget.Sherlock.ActionButton"> 
    <item name="background">@drawable/transparent</item> 
</style> 

@drawable/transparent是一個1x1透明PNG。只需在您的Manifest中將您的主題設置爲BlankMenu即可。

如果您的目標只是將日期包圍在藍色背景中,那麼您也可以重新設計主題,以默認爲MenuItem提供藍色背景,並從ActionBar主題中刪除藍色部分。它可能會使它更可靠。

2

在XML中爲頭創建一個自定義佈局,爲此擴展/ findViewById並更新日期,然後在您的ActionBar對象上調用setCustomView(View)

ActionBar actionBar = getSupportActionBar(); 
LinearLayout actionBarView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.action_bar_view, null); 
((TextView) actionBarView.findViewById(R.id.textViewDate)).setText(CURRENT_DATE); 
actionBar.setCustomView(actionBarView);    
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); 

編輯:另外,如果你想從一個資源ID設置自定義視圖,你可以調用setCustomView(INT渣油),然後使用getCustomView作出的觀點,這將是,如果你更多有用的實際變化有數據需要更改多次或actionBar創建後。

相關問題