[英]ActionBar not displaying when extended to listactivity
如標題中所述,我在擴展到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。 請幫我。
謝謝。
最簡單的方法是將此添加到您的清單中,默認情況下顯示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可能會覆蓋您的頂級項目名單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.