如何自定义 PreferenceActivity 的标题?
我有同样的问题,就像这个问题: PreferenceActivity 中的自定义标题栏?
扩展 PreferenceActivity
后,我在onCreate()
中编写这段代码,它只显示一个空白的灰色标题。我认为这是一个错误(因为这个解决方案与 Activity 配合得很好)。
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main_pref);
编辑:
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" />
<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" />
<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" />
</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" />
</PreferenceScreen>
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" />
<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" />
<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" />
</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" />
</PreferenceScreen>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
addPreferenceFromResource()
之后调用setFeatureInt()
。call
setFeatureInt()
AFTERaddPreferenceFromResource()
.