【问题标题】:Show extra info for Preference screens when CheckboxPreference summary field is not enough long?当 CheckboxPreference 摘要字段不够长时显示首选项屏幕的额外信息?
【发布时间】:2011-01-25 09:46:59
【问题描述】:

我有一个屏幕,您可以在其中启用/禁用我的 Android 应用程序的模块。

为此,我使用 CheckboxPreference 屏幕。这一切都很好,但是如果添加的描述超过 2 行,汇总字段就会被截断。

假设每个模块有 4-5 行可用的描述,我想在帮助窗口中显示它。

我尝试将点击事件绑定到 CheckboxPreference,但它会触发整行,因此不仅在单击复选框时,而且在您单击该行的任何位置都会切换复选框。

所以现在我想知道这是否可以解决。因此,如果用户需要更多信息,只需点击文本,助手就会打开,如果想要切换设置,请点击复选框。

你会怎么做?我也愿意接受其他想法,如果它们确实有效的话。

【问题讨论】:

    标签: android views preferences checkboxpreference


    【解决方案1】:

    您也许可以创建自己的 CheckboxPreference 子类,从而为您提供更多空间。

    【讨论】:

    • 如果没有出色的教程,我认为我无法独自做到这一点。既然你知道更多的资源,你知道帮助我的教程吗?
    • 不,从课堂上看,我不太清楚自己该怎么做。在未来的书籍版本中要涵盖的一堆东西的另一个主题...... :-)
    【解决方案2】:

    Hmn 今天也遇到了同样的问题。 我的替代解决方案是只允许在摘要中显示更多文本。

    只需创建您自己的 CheckBoxPreference 子类,如下所示:

    public class CheckBoxPreferenceWithLongSummary extends CheckBoxPreference{
    
        public CheckBoxPreferenceWithLongSummary(Context context) {
            super(context);
        }
    
        public CheckBoxPreferenceWithLongSummary(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public CheckBoxPreferenceWithLongSummary(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
            summaryView.setMaxLines(10);
        }
    }
    

    然后在您的 PreferenceScreen 中(我假设您使用 xml 来设置 Preference 布局 - 不确定它是否可能完全以编程方式)只需将旧的 <CheckBoxPreference.../> 替换为您的新实现,例如 <com.example.whatever.CheckBoxPreferenceWithLongSummary ... />

    似乎对我来说工作得很好,尽管我不确定 findViewById(android.R.id.summary) 因为在 parentClass 中使用了 com.android.internal.R.id.summary 这似乎是不能从 Java 中直接访问?希望这不仅仅是巧合:)

    【讨论】:

    • 工作完美!你为我节省了几个小时,伙计。谢谢
    【解决方案3】:

    我找到了更适合我的情况的替代解决方案,因为我需要针对不同类型的偏好设置更长的摘要,而不仅仅是 CheckboxPreferences。

    • 我将文件 data/res/layout/preference.xml 从 SDK 复制到我的应用程序中的 res/layout/preference_long_summary.xml。 (我从 API 级别 7 中选择了一个,因为它似乎也适用于更高级别)
    • @+android:id/summary TextView 中删除了android:maxLines 属性
    • 从我的首选项中引用了此布局,以获取我需要更长摘要的首选项android:layout="@layout/preference_long_summary"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-13
      • 2010-10-06
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多