gpt4 book ai didi

android - 如何处理 Android 首选项中的长文本?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:27 25 4
gpt4 key购买 nike

背景

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

问题

有些首选项的标题很长,我无法缩短,而且我认为如果我对应用程序进行本地化(翻译成多种语言),我将面临同样的问题(例如德语,它有很长的词,有时)。

在这种情况下,您得到的只是文本的开头,然后是文本结尾的“...”(或更少的点,顺便说一句,这没有多大意义)。

例子:

enter image description here

我尝试过的

我知道 PreferenceActivity 是从 ListActivity 扩展而来的,所以我可以将其适配器更改为我想要的任何内容,但这会破坏它的工作方式。

我也知道我可以从每种类型的首选项类进行扩展,使用“onCreateView”方法来引用创建的 View ,然后访问它的 subview ,但这很奇怪,不是吗?我的意思是,这几乎就像假设它永远不会改变它的外观一样。

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

从每个偏好类中扩展,并在每个偏好类中使用:

...
@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 可能会改变偏好 View 的工作方式。

问题

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

是否可以让它有一个椭圆大小/选取框(这样它就会有一个动画来显示所有内容)?或者也许自动调整字体大小?或者将其设置为自动换行?或者允许用户滚动阅读其余文本的水平 ScrollView ?

是否有关于如何处理此类情况的约定?也许长按以显示 toast /对话框以查看整个文本?

最佳答案

这是我针对 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>

这里我特意把标题的TextView中的android:singleLine值从true改成了false。这样就可以了。

关于android - 如何处理 Android 首选项中的长文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15853773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com