![](/img/trans.png)
[英]LinearLayout visibility not 'GONE' and also doesn't show on android 4.4.4
[英]ActionBar doesn't show in Android KitKat 4.4.4
我正在使用Android開發應用程序,但是在Fragment中實現ActionBar時遇到問題。
這是我的片段的代碼:
public class TopicListFragment extends ListFragment {
private LinkedList<Topic> mTopics;
private static final String TAG = "TopicListFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mTopics = TopicLab.get(getActivity()).getTopics();
ArrayAdapter<Topic> adapter = new TopicAdapter(mTopics);
setListAdapter(adapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "Menu created");
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_topic_list, menu);
}
這是用於布局的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item android:id="@+id/menu_item_new_topic"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/new_topic"
app:showAsAction="ifRoom"></item>
</menu>
當我運行該應用程序時,ActionBar不會顯示,如果我按智能手機上的菜單按鈕,它會顯示在丑陋的botton行中,僅包含標題。
如您在Fragment類中所看到的,我插入了一行調試,但是它僅在按下菜單按鈕時才顯示消息,而在創建Activity時則不顯示消息。
我該如何解決? 每個答案將不勝感激! 謝謝!
編輯 - -
根據要求,這是styles.xml文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
將此代碼放在xml文件中
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
在下面的代碼中包含片段的活動
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
並在您的樣式中添加
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
很久以后,如果您在舊版本的操作欄測試中出現差距,這是一個安全的選擇:
final RelativeLayout mainLayout = view.findViewById(R.id.main_layout);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Rect rectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
mainLayout.setPadding(0, rectangle.top, 0, 0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.