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

问题描述 投票:11回答:3

背景

我正在创建一个具有一些设置的应用程序,我想使用内置的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);
  }

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

这个问题

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

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

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

android textview android-preferences preferenceactivity longtext
3个回答
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"
        />
        ...

接下来,为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>

在这里,我故意将标题为android:singleLineTextView值从true改为false。这样做了。


2
投票

似乎新的支持库(Android-X)版本的首选项修复了这个。

我需要做的就是从PreferenceFragmentCompat扩展。例:

gradle文件:

implementation 'androidx.preference:preference:1.0.0'

Manaktivitykkt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState == null)
            supportFragmentManager.beginTransaction().replace(android.R.id.content, PrefsFragment()).commit()
    }

    class PrefsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.settings, rootKey)
        }
    }
}

的settings.xml

<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                                      xmlns:tools="http://schemas.android.com/tools">
    <androidx.preference.PreferenceCategory android:title="general">
        <androidx.preference.Preference android:summary="someSummary"
                                        android:key="pref" android:title="someTitle" tools:title="donation"/>
    </androidx.preference.PreferenceCategory>
</androidx.preference.PreferenceScreen>

0
投票

我不想复制粘贴布局,所以我最终继承了首选项:

import android.content.Context;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomCheckboxPreference extends CheckBoxPreference {

  public CustomCheckboxPreference(Context context) {
    super(context);
  }

  public CustomCheckboxPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public CustomCheckboxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  public CustomCheckboxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  @Override
  public void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    TextView title = (TextView) holder.findViewById(android.R.id.title);
    if (title != null) {
      title.setSingleLine(false);
    }
  }

}

然后在你的prefs.xml中使用它:

<android.support.v7.preference.PreferenceScreen ...>
  <android.support.v7.preference.PreferenceCategory ...>
    <com.example.CustomCheckboxPreference
      ...
      />
© www.soinside.com 2019 - 2024. All rights reserved.