Android 如何以编程方式在actionBar Sherlock中设置文本
下面是我的应用程序标题,我想在“DD/MM”位置设置当前日期时间。如何以编程方式执行此操作?我用的是actionBarSherlock 以下是我设置背景的方式:Android 如何以编程方式在actionBar Sherlock中设置文本,android,actionbarsherlock,Android,Actionbarsherlock,下面是我的应用程序标题,我想在“DD/MM”位置设置当前日期时间。如何以编程方式执行此操作?我用的是actionBarSherlock 以下是我设置背景的方式: <style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar"> <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
<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>
@style/Widget.myteme.ActionBar
@style/Widget.myteme.ActionBar
@可拉拔/abs\U bg
#ffffff
我可能会误解,但您可以添加一个不起任何作用的菜单项,设置为该文本(尽管这可能是一种不好的方式)
我认为这是一种方法,尽管我不确定它看起来有多好
编辑:您还可以通过设置自定义背景,在按下菜单项时,删除操作栏中突出显示的蓝色部分。下面是一个快速的方法(假设您只有该菜单项):
@样式/自定义项
@可拉伸/透明
当@drawable/transparent
为1x1透明PNG时。只需在清单中将主题设置为BlankMenu
如果您的目标是只让日期周围有蓝色背景,那么您也可以修改主题,默认情况下为菜单项提供蓝色背景,并从ActionBar主题中删除蓝色部分。这可能会使它更可靠。我可能有点误解,但您可以添加一个不起任何作用的菜单项,设置为该文本(尽管这可能是一种不好的方式)
我认为这是一种方法,尽管我不确定它看起来有多好
编辑:您还可以通过设置自定义背景,在按下菜单项时,删除操作栏中突出显示的蓝色部分。下面是一个快速的方法(假设您只有该菜单项):
@样式/自定义项
@可拉伸/透明
当@drawable/transparent
为1x1透明PNG时。只需在清单中将主题设置为BlankMenu
如果您的目标是只让日期周围有蓝色背景,那么您也可以修改主题,默认情况下为菜单项提供蓝色背景,并从ActionBar主题中删除蓝色部分。这可能会使它更可靠。用XML为标题创建一个自定义布局,膨胀/findViewById并更新日期,然后调用ActionBar对象
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 resID),然后使用getCustomView对视图进行实际更改,如果您有需要多次更改的数据,或者在创建actionBar之后,这将更有用。为XML标题创建自定义布局,为此膨胀/findViewById并更新日期,然后调用ActionBar对象
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 resID),然后使用getCustomView对视图进行实际更改,如果您有需要多次更改的数据或在创建actionBar之后更改的数据,则更有用。如果您不需要使用任何选项菜单或任何其他actionBarSherlock功能,则无需使用此库。您可以将任何主题与NoTitleBar
一起使用,并创建自定义RelativeLayout
t创建标题
styles.xml:
<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">
<!--Anything -->
</style>
<?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>
您的布局.xml:
<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">
<!--Anything -->
</style>
<?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>
如果您不需要使用任何选项菜单或任何其他actionBarSherlock功能,则无需使用此库。您可以将任何主题与NoTitleBar
一起使用,并创建自定义RelativeLayout
t创建标题
styles.xml:
<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">
<!--Anything -->
</style>
<?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>
您的布局.xml:
<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">
<!--Anything -->
</style>
<?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>
能否提供一个如何构建标题的代码示例?标题是一个图像,还没有textview。我已经添加了代码。查看问题的编辑能否提供如何构建标题的代码示例?标题是一个图像,还没有textview。我已经添加了代码。查看问题的编辑