[英]Android Action Bar title disappears after populating custom ListView
我的問題是:啟動應用程序時,只有一個活動,操作欄會正確顯示應用程序標題“ ParseC”(內容區域仍為空白),但是在加載RSS並填充自定義列表視圖后,標題消失了。 這在API 16和23上進行了測試,效果相同。
onCreate()解析RSS feed; 完成后,onPostExecute將使用條目填充列表視圖,此刻,應用程序欄標題將消失。 如果我運行getSupportActionBar()。getTitle()。toString(),則會得到正確的答案(ParseC)。
這是我的MainActivity.java(onCreate):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = this;
new DownloadXmlTask().execute(URL);
}
和(onPostExecute):
@Override
protected void onPostExecute(String result) {
setContentView(R.layout.activity_main);
//Adapter code
ListView yourListView = (ListView) findViewById(R.id.listView);
// get data from the table by the ListAdapter
ListAdapter customAdapter = new ListAdapter(context, R.layout.itemlistrow, theEntries);
yourListView .setAdapter(customAdapter);
}
我期望標題停留在那兒,因為我沒有離開活動(我懷疑布局可能會發生錯誤)。
至於布局,主要活動布局(activity_main.xml)包括content_main.xml,該內容具有使用自定義布局itemlistrow.xml的listview。
我的問題是:什么可能導致App Bar標簽被隱藏?
下面的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.samoliver.parsec">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="co.samoliver.parsec.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_info" />
布局:content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="co.samoliver.parsec.MainActivity"
tools:showIn="@layout/activity_main">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView" />
itemlistrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_width="fill_parent">
<TextView android:textColor="#000000"
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" android:textStyle="bold"
android:gravity="center_vertical"
android:layout_weight="1"
android:typeface="normal"
android:height="40sp"
android:textSize="16dp" />
<TextView android:textColor="#000000"
android:id="@+id/summary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Summary"
android:layout_weight="1"
android:height="20sp"
android:typeface="normal" />
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textColor="#000000"
android:gravity="right"
android:id="@+id/id"
android:text="Id"
android:height="0sp"
android:typeface="normal" />
錯誤可能在您的清單android:theme="@style/AppTheme.NoActionBar"
。 另外,您不需要調用setContentView()
兩次(這也可能導致您的問題)。 一旦在onCreate()
應該這樣做。 如果您不調用兩次,而是在收到數據時調用適配器上的notifyDataSetChanged()
,它將提高應用程序的性能。 我建議您在onCreate()
本身中設置適配器,並在接收到數據時對其進行更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.