Android 为什么不';当我在PreferenceActivity中使用Theme.AppCompat.Light.DarkActionBar时,应用程序名称是否显示?
我已经设置了android:label=“@string/app_name”,当类扩展AppCompatActivity时,可以在CleanupAbout.java中正确显示app名称 我定制了一个PreferenceActivity CleanupReference.java,它扩展了PreferenceActivity,但我发现应用程序名称消失了,为什么?谢谢 CleanupAbout.java中的应用程序名称正常Android 为什么不';当我在PreferenceActivity中使用Theme.AppCompat.Light.DarkActionBar时,应用程序名称是否显示?,android,android-studio,Android,Android Studio,我已经设置了android:label=“@string/app_name”,当类扩展AppCompatActivity时,可以在CleanupAbout.java中正确显示app名称 我定制了一个PreferenceActivity CleanupReference.java,它扩展了PreferenceActivity,但我发现应用程序名称消失了,为什么?谢谢 CleanupAbout.java中的应用程序名称正常 <?xml version="1.0" encoding="utf-8
<?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>
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);
}
}
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);
}
}
应用程序名称在CleanupReference.java中消失
<?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>
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);
}
}
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\u preference\u resource.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>
<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>
<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>
<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>
<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>
cleanup_about.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>
<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>
<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>
<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>
<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>
您可以使用
PreferenceFragment
并将其添加到您的cleanup\u 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>
不要忘记在依赖项中包含appcompat v7
compile 'com.android.support:appcompat-v7:22.2.0'
谢谢但也许你的代码不正确。为了使用setSupportActionBar,您必须使用一个从ActionBarActivity扩展的类,目前我的类扩展了referenceActivity
PreferenceActivity
扩展了plainActivity
不支持AppCompatActivity
,因此您不能期望它的行为与您的其他活动完全相同。你需要这个。把这个类放在你的项目中,让你的偏好活动扩展它。我的错误,请参阅更新的答案。实际上,我使用PreferenceFragment
来显示我的应用程序设置。