【问题标题】:Android preference summary . How to set 3 lines in summary?Android偏好总结。如何在摘要中设置 3 行?
【发布时间】:2011-10-07 10:52:56
【问题描述】:

偏好摘要仅允许 2 行。 如果我想在摘要中显示 3 行或更多行。我能怎么做 ?

【问题讨论】:

  • 您好,您正在使用 TextView 或其他功能显示内容

标签: android preference summary


【解决方案1】:

您可以通过扩展任何现有首选项来创建 Preference 类:

public class LongSummaryCheckboxPreference extends CheckboxPreference
{
    public LongSummaryCheckboxPreference(Context ctx, AttributeSet attrs, int defStyle)
    {
        super(ctx, attrs, defStyle);        
    }

    public LongSummaryCheckboxPreference(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);  
    }

    @Override
    protected void onBindView(View view)
    {       
        super.onBindView(view);

        TextView summary= (TextView)view.findViewById(android.R.id.summary);
        summary.setMaxLines(3);
    }       
}

然后在preferences.xml:

 <com.your.package.name.LongSummaryCheckBoxPreference 
    android:key="@string/key"
    android:title="@string/title"
    android:summary="@string/summary" 
    ... />

缺点是您需要将所有需要 3 行摘要的偏好类型子类化。

【讨论】:

  • 工作出色并点赞!!。如果你使用androidx.preference,你可以使用onBindViewHolder而不是onBindView
【解决方案2】:

使用androidx.preference.PreferenceCategory 我得到了类似的东西:

Java:

public class LongSummaryPreferenceCategory extends PreferenceCategory {

    public LongSummaryPreferenceCategory(Context ctx, AttributeSet attrs, int defStyle) {
        super(ctx, attrs, defStyle);
    }

    public LongSummaryPreferenceCategory(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        TextView summary= (TextView)holder.findViewById(android.R.id.summary);
        if (summary != null) {
            // Enable multiple line support
            summary.setSingleLine(false);
            summary.setMaxLines(10); // Just need to be high enough I guess
        }
    }
    
}

科特林:

class LongSummaryPreferenceCategory @JvmOverloads constructor(
  context: Context, 
  attrs: AttributeSet? = null
): PreferenceCategory(context, attrs) {

  override fun onBindViewHolder(holder: PreferenceViewHolder) {
    super.onBindViewHolder(holder)
    val summary = holder.findViewById(android.R.id.summary) as? TextView
    summary?.let {
      // Enable multiple line support
      summary.isSingleLine = false
      summary.maxLines = 10 // Just need to be high enough I guess
    }
  }
}

【讨论】:

  • 工作正常,我已将其转换为 Kotlin 并添加了 Kotlin 版本:)
猜你喜欢
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 2013-03-27
相关资源
最近更新 更多