繁体   English   中英

填充自定义ListView后,Android操作栏标题消失

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM