2015-06-21 43 views
0

我已经设置android:label =“@ string/app_name”,当类扩展AppCompatActivity时,应用程序名可以在CleanupAbout.java中正确显示。当我在PreferenceActivity中使用Theme.AppCompat.Light.DarkActionBar时,为什么不显示应用程序名称?

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

程序名称确定在CleanupAbout.java

enter image description here

应用程序名称在CleanupPreference.java消失

enter image description here

<?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> 

Style.xml

<?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> 

CleanupPreference.java

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_resource.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> 

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"> 

    <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> 

my_preference_item.xml

<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> 

CleanupAbout.java

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); 

    } 

} 

cleanup_about.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" /> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/adView" 
     android:layout_above="@+id/linearLayout1" 
     android:paddingTop="12dip" 
     android:paddingLeft="7dip" 
     android:paddingRight="7dip" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/appversion" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/tvVersion" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="2dip" 
      android:text="" /> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/myabout" 
      android:layout_marginTop="17dip" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/tvMyLink" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="2dip" 
      android:text="@string/mylink" /> 


    </LinearLayout> 

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

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

</RelativeLayout> 

回答

0

您可以使用PreferenceFragment,并添加toolbarcleanup_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> 

,并在您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); 
     } 
    } 
} 

设置toolbaractionbar不要忘记包括appcompat v7您关联

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

感谢。但是也许你的代码不正确。为了使用setSupportActionBar,你必须使用一个从ActionBarActivity扩展的类,目前我的类扩展了referenceActivity – HelloCW

+1

'PreferenceActivity'扩展了普通'Activity'不支持'AppCompatActivity',所以你不能指望它像你的其他活动一样行事。你需要['AppCompatPreferenceActivity'](https:// github。com/android/platform_development/blob/master/samples/Support7Demos/src/com/example/android/supportv7/app/AppCompatPreferenceActivity.java)。把这个类放到你的项目中,让你的偏好活动扩展它。 –

+0

我的错误,看到更新的答案。其实我使用'PreferenceFragment'来显示我的应用程序设置。 –

相关问题