如何在Android上的首选项中处理长文本?

and*_*per 12 android textview longtext preferenceactivity android-preferences

背景

我正在创建一个具有一些设置的应用程序,我想使用内置的PreferenceActivity或PreferenceFragment来完成工作

问题

一些偏好有一个很长的标题,我无法缩短,而且我认为如果我本地化应用程序(翻译成多种语言),我将面临同样的问题(例如在德语中,有时会有很长的单词).

你在这种情况下得到的只是文本的开头,然后是"......"(或更少的点,这对btw没有多大意义).

例:

在此输入图像描述

我试过的

我知道PreferenceActivity从ListActivity扩展,所以我可以将其适配器更改为我想要的任何东西,但这将删除它的工作方式.

我也知道我可以从每个类型的首选项类扩展,使用"onCreateView"方法来引用创建的视图,然后访问它的子类,但这很奇怪,不是吗?我的意思是,它几乎就像假设它永远不会改变它的外观.

编辑:这是我尝试过的示例代码:

从每个首选项类扩展,并在每个首选项中使用:

...
@Override
protected View onCreateView(final ViewGroup parent)
  {
  final View view=super.onCreateView(parent);
  ViewUtil.handlePreferenceTitleTextView(view);
  return view;
  }
...

//ViewUtil.java :

private void handlePreferenceTitleTextView(final View v)
  {
  final TextView titleTextView=(TextView)v.findViewById(android.R.id.title);
  if(titleTextView!=null)
    titleTextView.setSingleLine(false);
  }
Run Code Online (Sandbox Code Playgroud)

它有效,但我不认为这是推荐的,因为Google可能会改变首选项视图的工作方式.

这个问题

如何在Android上的首选项标题中处理长文本?

是否有可能使它具有椭圆形/选框(以便它将有一个动画来显示所有内容)?或者也许自动适合字体大小?或者将其设置为自动换行?还是一个水平scrollView,允许用户滚动阅读文本的其余部分?

是否可能有如何处理此类案件的惯例?也许长按一下可以看到整个文本的吐司/对话框?

Sta*_*tan 5

这是我针对特定情况SwitchPreference和一般偏好的问题的解决方案.

首先,我应该注意到,对于14级之前的API级别,默认情况下首选项标题是多行的 - 至少我没有看到Android 2.3.3中长标题的任何问题.在较新版本的Android中,此行为已更改为强制单行标题.

解决方法是稍微调整一下SwitchPreference或任何其他类型的首选项布局.

在首选项屏幕文件中添加android:layout所需首选项的属性,例如:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <PreferenceCategory android:title="@string/prefs_category">
    <SwitchPreference
        android:key="keyOfThePreference"
        android:title="@string/pref_title"
        android:switchTextOn="@string/pref_on"
        android:switchTextOff="@string/pref_off"
        android:summaryOn="@string/pref_enabled"
        android:summaryOff="@string/pref_disabled"
        android:layout="@layout/preference_multiline"
        />
        ...
Run Code Online (Sandbox Code Playgroud)

接下来,提供preference_multiline.xml替代布局.我使用了标准的修改版本preference.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a Preference in a PreferenceActivity. The
     Preference is able to place a specific widget for its particular
     type in the "widget_frame" layout. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground" >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在这里,我特意改变android:singleLine在标题的值TextViewtruefalse.这样做了.


Mer*_*n E 2

2021:当使用 androidx.preference 时(可能每个人都有)

你可以简单地使用app:singleLineTitle="false"

<Preference
        app:key="your_key"
        app:singleLineTitle="false"
        app:title="LONG TITLEEEEEEE EEEEEEEEE EEEEEEEEEEEEEEE" />
Run Code Online (Sandbox Code Playgroud)

也适用于开关等