简体   繁体   English

两行 Android PreferenceScreen 标题

[英]Android PreferenceScreen title in two lines

I have PreferenceScreen with long title especially in some languages.我的 PreferenceScreen 标题很长,尤其是在某些语言中。 I'm able to set multiple lines title for CheckBoxPreference or ListPreference with this: Android preference summary .我可以使用以下内容为 CheckBoxPreference 或 ListPreference 设置多行标题: Android 偏好摘要。 How to set 3 lines in summary? 如何总结设置3行? , but how to set 2-lines title for PreferenceScreen? ,但如何为 PreferenceScreen 设置 2 行标题? I can change style like here: How can I change font size in PreferenceScreen but this doesn't look perfect and it's inconsistent with preference style (font, size ...).我可以像这样更改样式: 如何在 PreferenceScreen 中更改字体大小,但这看起来并不完美,并且与首选项样式(字体、大小...)不一致。

thanks!谢谢!

By default the title is set to single line.默认情况下,标题设置为单行。 You need to extend Preference and get title textview and set single line to false.您需要扩展 Preference 并获取标题 textview 并将单行设置为 false。 Use this class instead of the regular PreferenceScreen:使用这个类而不是常规的 PreferenceScreen:

public class TwoLinePreference extends Preference {

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

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

  public TwoLinePreference(Context ctx) {
    super(ctx);
  }

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

    TextView textView = (TextView) view.findViewById(android.R.id.title);
    if (textView != null) {
        textView.setSingleLine(false);
      }
  }
}

Use the method setSingleLineTitle(false)使用方法setSingleLineTitle(false)

This was added in API 26, so you should be able to use the support library version for older devices这是在 API 26 中添加的,因此您应该能够使用旧设备的支持库版本

If you want just for a single preference, using setSingleLineTitle or app:singleLineTitle="false" in the XML file.如果您只需要一个首选项,请在 XML 文件中使用setSingleLineTitleapp:singleLineTitle="false"

If you wish to apply it on all preferences, you can do it in multiple ways, when extending PreferenceFragmentCompat :如果您希望将其应用于所有首选项,则可以在扩展PreferenceFragmentCompat时以多种方式执行此操作:

1.going over all preferences and setting it: 1.查看所有首选项并进行设置:

override fun setPreferenceScreen(preferenceScreen: PreferenceScreen?) {
    if (preferenceScreen != null)
        setAllPreferencesToHaveMultiLineTitles(preferenceScreen)
    super.setPreferenceScreen(preferenceScreen)
}

private fun setAllPreferencesToHaveMultiLineTitles(preference: Preference) {
    preference.isSingleLineTitle = false
    if (preference is PreferenceGroup)
        for (i in 0 until preference.preferenceCount)
            setAllPreferencesToHaveMultiLineTitles(preference.getPreference(i))
}

2.Doing the same in the adapter, but not recommended as it might not work some day (reaches hidden API of the library) : 2.在适配器中做同样的事情,但不推荐,因为它可能有一天不起作用(到达库的隐藏API):

override fun onCreateAdapter(preferenceScreen: PreferenceScreen?): RecyclerView.Adapter<*> {
    return object : PreferenceGroupAdapter(preferenceScreen) {
        @SuppressLint("RestrictedApi")
        override fun getItem(position: Int): Preference {
            val item = super.getItem(position)
            item.isSingleLineTitle = false
            return item
        }
    }
}

3.Similar, but without using isSingleLineTitle (but again not recommended for the same reason) : 3.类似,但不使用isSingleLineTitle (但出于同样的原因再次不推荐):

override fun onCreateAdapter(preferenceScreen: PreferenceScreen?): RecyclerView.Adapter<*> {
    return object : PreferenceGroupAdapter(preferenceScreen) {
        @SuppressLint("RestrictedApi")
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PreferenceViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            setPreferenceTitleTextViewToHaveMultipleLines(holder.itemView)
            return holder
        }
    }
}

fun setPreferenceTitleTextViewToHaveMultipleLines(v: View) {
    if (v is TextView && v.getId() == android.R.id.title)
        return v.setSingleLine(false)
    if (v is ViewGroup)
        for (i in 0 until v.childCount)
            setPreferenceTitleTextViewToHaveMultipleLines(v.getChildAt(i))
}

androidx.preferece库在标题和摘要中默认不再有单行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM