13

バックグラウンド

いくつかの設定を持つアプリを作成しており、ジョブに組み込みの PreferenceActivity または PreferenceFragment を使用したい

問題

一部の設定には、短縮できない長いタイトルがあり、アプリをローカライズ (複数の言語に翻訳) すると、同じ問題に直面すると思います (たとえば、非常に長い単語を含むドイツ語など)。

この状況で得られるのは、テキストの始まりに過ぎず、最後に "..." (またはドットが少ないため、あまり意味がありません) です。

例:

ここに画像の説明を入力

私が試したこと

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 の環境設定のタイトルで長いテキストを処理するにはどうすればよいですか?

楕円形/マーキーを持たせることは可能ですか (すべてを表示するアニメーションを持つように) ? または、フォントサイズを自動調整しますか?またはワードラップを持つように設定しますか?または、ユーザーがスクロールして残りのテキストを読むことができる水平スクロールビューですか?

そのような場合を処理する方法の慣例はありますか?テキスト全体を表示するために、長押ししてトースト/ダイアログを表示することはありますか?

4

4 に答える 4

6

これは、特定のケースとSwitchPreference一般的な好みの問題に対する私の解決策です。

まず第一に、14 より前の API レベルでは、プリファレンス タイトルがデフォルトで複数行になっているように見えることに注意してください。少なくとも、Android 2.3.3 では長いタイトルに問題は見られませんでした。Android の新しいバージョンでは、この動作は強制的な 1 行のタイトルに変更されました。

回避策は、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:singleLineで、タイトルの値を意図的にTextViewからtrueに変更しましたfalse。これは仕事をします。

于 2014-09-26T10:48:31.663 に答える
2

hereおよびhereのソリューションに基づいて、これは、必要に応じてすべての人に設定する方法です。

abstract class BasePreferenceFragment : PreferenceFragmentCompat() {

    private fun applyOperationsForAllPreferences(preference: Preference) {
        // preference.isIconSpaceReserved = false //you can add this too, if you don't want icons space
        preference.isSingleLineTitle = false
        if (preference is PreferenceGroup)
            for (i in 0 until preference.preferenceCount)
                applyOperationsForAllPreferences(preference.getPreference(i))
    }

    override fun setPreferenceScreen(preferenceScreen: PreferenceScreen?) {
        preferenceScreen?.let { applyOperationsForAllPreferences(it) }
        super.setPreferenceScreen(preferenceScreen)
    }

}
于 2019-02-23T22:28:59.060 に答える