Android 填充自定义 ListView 后操作栏标题消失
Android Action Bar title disappears after populating custom ListView
我的问题是:启动应用程序时,只有一个 activity,操作栏正确显示应用程序标题 "ParseC"(而内容区域仍为空白),但在加载 RSS 并填充自定义列表视图后,标题消失了。这在 API 16 和 23 上进行了测试,效果相同。
onCreate() 解析 RSS 提要;完成后,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(我怀疑错误可能发生在布局上)。
至于布局,主要 activity 布局 (activity_main.xml) 包括 content_main.xml,它具有使用自定义布局的列表视图 itemlistrow.xml。
我的问题是:什么可能导致应用栏标签被隐藏?
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()
本身中设置您的适配器,并在收到数据时更新它。
我的问题是:启动应用程序时,只有一个 activity,操作栏正确显示应用程序标题 "ParseC"(而内容区域仍为空白),但在加载 RSS 并填充自定义列表视图后,标题消失了。这在 API 16 和 23 上进行了测试,效果相同。
onCreate() 解析 RSS 提要;完成后,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(我怀疑错误可能发生在布局上)。
至于布局,主要 activity 布局 (activity_main.xml) 包括 content_main.xml,它具有使用自定义布局的列表视图 itemlistrow.xml。
我的问题是:什么可能导致应用栏标签被隐藏?
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()
本身中设置您的适配器,并在收到数据时更新它。