【问题标题】:Custom preference layout widget not visible自定义首选项布局小部件不可见
【发布时间】:2023-03-09 03:26:02
【问题描述】:

编辑

对于遇到此问题的任何人,我的工作方式是:

直接为每个首选项设置布局,如:

<CheckboxPreference
    android:layout="@layout/checkbox_preference_item"/>

或者像我之前做的那样通过主题设置样式,但定义一个小部件布局并设置它而不是实际布局,就像这样,其中preferenceStyle设置为@style/PreferencecheckBoxPreferenceStyle设置为@ 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


    【解决方案1】:

    我在布局 xml 中没有看到 CheckBox。我错过了什么吗?您需要指定如下内容:

    <CheckBox android:id="..." ... />
    

    那么您可能需要指定类似

    的内容
    <CheckBoxPreference android:key="..." ... />
    

    在首选项 xml 文件中。设置应该是 android:widgetLayout 到自定义 CheckBox。

    看到这篇文章可能对你有更多帮助customizing checkbox preference

    【讨论】:

    • 我的印象是复选框会自动添加到 widget_frame 线性布局中,仿照此:github.com/android/platform_frameworks_base/blob/master/core/… 我实际上并没有尝试自定义复选框本身,只是将容纳的容器它
    • 在这种情况下 - 请参阅 link - 我认为 layout_height 可能会限制复选框的可见性 -
    • 奇怪...如果我直接在 settings.xml 中为首选项设置布局,就像在那个链接中一样,它可以工作,但是如果我只是尝试为所有首选项设置样式(所以我不必为每个人都这样做)它不起作用。不过谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    相关资源
    最近更新 更多