如标题中所述,我在扩展到listactivity时显示操作栏时遇到问题。
正如您在下面所见,我尝试过requestwindowfeature,但它仍未显示。
public class AlarmListActivity extends ListActivity {
private AlarmListAdapter mAdapter;
private AlarmDBHelper dbHelper = new AlarmDBHelper(this);
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.activity_alarm_list);
mContext = this;
mAdapter = new AlarmListAdapter(this, dbHelper.getAlarms());
setListAdapter(mAdapter);
//ActionBar actionBar = getSupportActionBar();
//actionBar.setLogo(R.drawable.ic_launcher);
//actionBar.setDisplayUseLogoEnabled(true);
//actionBar.setDisplayShowHomeEnabled(true);
}
styles.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">?android:attr/listDivider</item>
</style>
清单
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".HomeScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="GOHOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SecondMainActivity"
android:label="@string/title_activity_second_main" >
<intent-filter>
<action android:name="GOHOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.kaixian.ticmeds.alarmclock.AlarmListActivity"
android:label="@string/app_name" >
</activity>
<activity android:name="com.example.kaixian.ticmeds.alarmclock.AlarmScreen" />
<activity android:name="com.example.kaixian.ticmeds.alarmclock.AlarmDetailsActivity" />
<service android:name="com.example.kaixian.ticmeds.alarmclock.AlarmService"
android:enabled="true"/>
<receiver android:name="com.example.kaixian.ticmeds.alarmclock.AlarmManagerHelper" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
Activity_alarm_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AlarmListActivity" >
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
有没有办法在扩展到ListActivity时显示ActionBar,我需要它扩展到ListActivity。请帮帮我。
谢谢。
答案 0 :(得分:0)
最简单的方法是将此添加到您的清单中,默认情况下显示ActionBar您应该使用A Holo Theme或其后代之一:
<activity
android:name="com.example.kaixian.ticmeds.alarmclock.AlarmListActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo" />
如果您的ActionBar有一个菜单,您可以在ListActivity中对其进行充气,如下所示:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return super.onCreateOptionsMenu(menu);
}
我建议同时执行这两项操作,因为如果您不将主题添加到Manifest并且只通过onCreateOptionsMenu()进行充气,则可能无法正确显示Listactivity,因为它不会处理paddingTop,因此您的ActionBar可能会覆盖顶部你的清单上的项目。