preferenceActivity的自定义标题(的问题)自定义、标题、问题、preferenceActivity

2023-09-05 03:38:02 作者:既然没有既然何必再说何必

我有这样的问题,同样的问题: 在preferenceActivity 自定义标题栏?

延长preferenceActivity后,我写这篇code中的onCreate(),它只是显示一个空白的灰色标题。我认为这是一个错误(因为该解决方案可以很好地活动)。

  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_title);
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml.main_ preF);
 

编辑

window_title.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =match_parent
    机器人:背景=@颜色/标题栏的
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=center_vertical
    机器人:以下属性来=5dip
    机器人:paddingRight =5dip>
    < ImageView的机器人:ID =@ + ID / imageView1
               机器人:SRC =@可绘制/ megadict_icon
               机器人:layout_height =35dip
               机器人:layout_width =35dip
               机器人:layout_gravity =center_vertical>
    < / ImageView的>
    < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
              机器人:ID =@ + ID / textView1
              机器人:layout_height =WRAP_CONTENT
              机器人:文字颜色=@色/白
              机器人:TEXTSIZE =16dip
              机器人:layout_weight =1
              机器人:layout_gravity =center_vertical
              机器人:文本=@字符串/ APPNAME」
              机器人:以下属性来=5dip
              机器人:paddingRight =5dip>
    < / TextView的>
    <进度风格=机器人:ATTR / progressBarStyleSmall
                 机器人:ID =@ + ID /进度
                 机器人:layout_width =28DIP
                 机器人:layout_height =28DIP
                 机器人:layout_gravity =center_vertical
                 机器人:能见度=隐形>
    < /进度>
< / LinearLayout中>
 

main_ pref.xml

 < XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:标题=@字符串/主prefTitle>
    <表preference安卓项=@阵列/ languageStrings
                    机器人:entryValues​​ =@阵列/ languageValues​​
                    机器人:dialogTitle =@字符串/语言prefTitle
                    机器人:标题=@字符串/主prefTitle安卓键=语言prefKey>
    < /目录preference>
< / preferenceScreen>
 
Android之PreferenceActivity 详解

解决方案

呼叫 setFeatureInt()添加preferenceFromResource()

I have the same problem like this question: Custom title bar in PreferenceActivity ??

After extending PreferenceActivity, I write this code in onCreate(), it just shows a blank grey title. I think it is a bug (because this solution works well with Activity).

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main_pref);

Edited:

window_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="@color/titleBar" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:paddingLeft="5dip" 
    android:paddingRight="5dip">
    <ImageView android:id="@+id/imageView1" 
               android:src="@drawable/megadict_icon" 
               android:layout_height="35dip" 
               android:layout_width="35dip" 
               android:layout_gravity="center_vertical">
    </ImageView>
    <TextView android:layout_width="wrap_content" 
              android:id="@+id/textView1" 
              android:layout_height="wrap_content" 
              android:textColor="@color/white" 
              android:textSize="16dip" 
              android:layout_weight="1" 
              android:layout_gravity="center_vertical" 
              android:text="@string/appName" 
              android:paddingLeft="5dip" 
              android:paddingRight="5dip">
    </TextView>
    <ProgressBar style="?android:attr/progressBarStyleSmall" 
                 android:id="@+id/progressBar" 
                 android:layout_width="28dip" 
                 android:layout_height="28dip" 
                 android:layout_gravity="center_vertical" 
                 android:visibility="invisible">
    </ProgressBar> 
</LinearLayout>

main_pref.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android" android:title="@string/mainPrefTitle">
    <ListPreference android:entries="@array/languageStrings" 
                    android:entryValues="@array/languageValues" 
                    android:dialogTitle="@string/languagePrefTitle" 
                    android:title="@string/mainPrefTitle" android:key="languagePrefKey">
    </ListPreference>    
</PreferenceScreen>

解决方案

call setFeatureInt() AFTER addPreferenceFromResource().