首选项摘要仅允许 2 行。如果我想在摘要中显示 3 行或更多行。我能怎么做 ?
问问题
5446 次
2 回答
25
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 行摘要的所有首选项类型子类化。
于 2011-07-18T07:03:37.543 回答
5
使用androidx.preference.PreferenceCategory
我得到了类似的东西:
爪哇:
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
}
}
}
于 2020-03-09T10:37:04.737 回答