当我在PreferenceActivity中使用Theme.AppCompat.Light.DarkActionBar时,为什么不显示应用程序名称?

时间:2015-06-21 14:26:15

标签: android android-studio

我已经设置了android:label =“@ string / app_name”,当类扩展AppCompatActivity时,可以在CleanupAbout.java中正确显示应用名称。

我定制了PreferenceActivity CleanupPreference.java,它扩展了PreferenceActivity,但我发现应用名称消失了,为什么?谢谢!

CleanupAbout.java中的应用名称确定

enter image description here

App名称在CleanupPreference.java中消失

enter image description here

20

Style.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.dodata.messagecleanup" >

    <
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="ui.CleanupMain"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name="ui.CleanupAbout"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="ui.CleanupAbout" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


        <activity android:name="ui.CleanupPreference"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="ui.CleanupPreference" />
                <category android:name="android.intent.category.PREFERENCE" />
            </intent-filter>
        </activity>

    </application>

</manifest>

CleanupPreference.java

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="myTextLarge">
        <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
        <item name="android:textSize">21sp</item>
    </style>

    <style name="myTextMedium">
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:textSize">16sp</item>
    </style>

    <style name="myTextSmall">
        <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
    </style>

    <style name="myTextDesciption">
        <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
        <item name="android:textColor">@color/violet</item>
    </style>

</resources>

cleanup_preference_resource.xml

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CleanupPreference extends PreferenceActivity{
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.cleanup_preference_resource);
        setContentView(R.layout.cleanup_preference);
    }

}

cleanup_preference.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="AppPreference"
    android:summary="@string/PreferenceSummary"
    android:title="@string/Preference" >


    <CheckBoxPreference
       android:defaultValue="true"
       android:key="IsWriteToLog"
       android:title="@string/WriteToLogTitle"
       android:summary="@string/WriteToLogSummary"
       android:layout="@layout/my_preference_item"
    />    

</PreferenceScreen>

my_preference_item.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/border_ui"
    android:orientation="vertical">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        android:layout_alignParentTop="true"
        ads:adUnitId="@string/ad_unit_id" />


    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/adView"
        android:layout_above="@+id/linearLayout1"
        >
    </ListView>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="4" >

        <Button
            android:id="@+id/btnClosePreference"
            style="@style/myTextMedium"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:text="@string/BtnReturn" >
        </Button>
    </LinearLayout>

</RelativeLayout>

CleanupAbout.java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="8dip"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="0dp"
        android:orientation="horizontal"
        android:visibility="gone" >

        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minWidth="48dp"
            android:paddingRight="8dip" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingBottom="6dip"
        android:paddingRight="8dip"
        android:paddingTop="6dip" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            style="@style/myTextMedium" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            style="@style/myTextSmall"
            android:textColor="?android:attr/textColorSecondary" />
    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->

    <LinearLayout
        android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="48dp"
        android:orientation="vertical" />

</LinearLayout>

cleanup_about.xml

import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CleanupAbout extends AppCompatActivity {

    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cleanup_about);  

    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用PreferenceFragment并将toolbar添加到cleanup_preference.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/border_ui"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/Theme.AppCompat.Light" >

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_below="@+id/toolbar"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        android:layout_alignParentTop="true"
        ads:adUnitId="@string/ad_unit_id" />

    ......

</RelativeLayout>

并在toolbar

中将actionbar设为CleanupPreference.java
public class CleanupPreference extends AppCompatActivity {

    ......

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cleanup_preference);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new SettingsFragment())
                .commit();
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.cleanup_preference_resource);
        }
    }
}

并且不要忘记在您的依赖项中包含appcompat v7

compile 'com.android.support:appcompat-v7:22.2.0'