【发布时间】:2023-03-09 03:26:02
【问题描述】:
编辑:
对于遇到此问题的任何人,我的工作方式是:
直接为每个首选项设置布局,如:
<CheckboxPreference
android:layout="@layout/checkbox_preference_item"/>
或者像我之前做的那样通过主题设置样式,但定义一个小部件布局并设置它而不是实际布局,就像这样,其中preferenceStyle设置为@style/Preference,checkBoxPreferenceStyle设置为@ 987654325@:
<Style name="Preference">
<item name="android:layout">@layout/checkbox_preference_item</item>
</Style>
<Style name="Preference.Checkbox">
<item name="android:widgetLayout">@layout/your_custom_widget</item>
</Style>
我正在尝试自定义复选框首选项项目的布局,但复选框本身没有显示。我正在模仿 android 代码,但不知道哪里出错了。文本视图以及布局的其余部分工作正常,但复选框未显示。对此有任何帮助将不胜感激。
相关代码:
theme.xml:
<style name="Theme.Settings" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/SettingsActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:preferenceCategoryStyle">@style/PreferenceCategory</item>
<item name="android:preferenceStyle">@style/Preference</item>
<item name="android:checkBoxPreferenceStyle">@style/Checkbox</item>
</style>
style.xml
<style name="Checkbox">
<item name="android:layout">@layout/checkbox_preference_item</item>
</style>
<style name="Preference.Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">@dimen/preferences_item_font</item>
<item name="android:textColor">@color/preference_text</item>
</style>
<style name="Preference.Text.Extra" parent="Preference.Text">
<item name="android:textSize">@dimen/preferences_header_font</item>
</style>
checkbox_preference_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/preferences_item_height"
android:background="@color/white"
android:paddingLeft="@dimen/preferences_item_left">
<LinearLayout android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical">
<TextView android:id="@android:id/title"
style="@style/Preference.Text"/>
<TextView android:id="@android:id/summary"
style="@style/Preference.Text.Extra"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:id="@android:id/widget_frame"
android:layout_alignParentRight="true"/>
</RelativeLayout>
【问题讨论】:
标签: android android-layout android-activity android-fragments android-preferences