2012-07-04 15 views
13

私はPreferencesActivityを持っています。私はアラビア語を使用したいので右揃えにする必要があります。PreferenceScreenandroid:layout_gravity="right"を使用しようとしましたが、動作しませんでした。PreferencesActivityをAndroidで右揃えにする方法は?

これは私のXMLです:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="right"> 
     <PreferenceCategory android:title="General Settings"> 
       <CheckBoxPreference 
         android:title="Full Screen" 
         android:defaultValue="false" 
         android:summary="Always view as Full Screen" 
         android:key="fullScreenPref" /> 
     <Preference 
       android:title="Report Bugs" 
       android:summary="Notify us for any Bugs or Errors" 
       android:key="bugs"/> 
     <Preference 
       android:title="About" 
       android:summary="Version 1.0.0" 
       android:key="about"/> 
     </PreferenceCategory> 
</PreferenceScreen> 

これは私がPreferencesActivity内でXMLを使用する方法である:

addPreferencesFromResource(R.layout.preferences); 
+0

今日はあなたを助けてくれるはずはありませんが、Googleは最近、右から左に書く言語で大幅な改善を行いました。残念ながら、JellyBeanより前のデバイスには表示されません – Merlin

答えて

9

残念ながら基本プリファレンスコンポーネント(PreferenceScreenPreferenceなど)で実装されていますかなり設定不可能な方法です。彼らは特定の方法で動作するように設計されており、ひどくカスタマイズすることはできません。

あなたが望むようにするには、独自のバージョンのPreferenceScreen(または、CheckBoxPreferenceなどの使用している嗜好タイプのみ)を実装する必要があります。それがXMLレイアウトを膨らませるときには、XMLレイアウトについて多くのことを想定し、あなたのために扱います(テキストサイズ、パディング、レイアウト、ラッピングなど)。これらの設定を調整する必要がある場合は、デフォルトのように見えるようにしたい場合はこれは素晴らしいことですが、それほど素晴らしいものではありません。

(注意:あなたは、単にリストビューを使用して、好みのページとしてそれを扱うことができpreferencescreen実装したくない場合は、あなたは基本的には好みの独自のバージョンを実装する必要がいずれかの方法を)

(注2:私がアラビア語のテキストについて見た他の質問に基づいて、それはPOSSIBLEです。アンドロイドはデフォルトでそれを右揃えしようとするほどスマートですが、それでも驚いています。 。)

申し訳ありません申し訳ありませんがあなたのために良い答えがありません。

+3

アラビア語で書くとAndroidスマートフォンで十分にスマートになります(一般設定)。 –

+0

これは、環境設定が拡張される方法によって決まるデフォルト設定です。私が知る限り、それを変更したい場合は、あなた自身のバージョンのプリファレンスを書く必要があります。 – matt5784

+0

独自の設定レイアウトxmlを作成し、テキストを右揃えにすることができます。これを見てください:http://stackoverflow.com/questions/7094584/checkboxpreference-with-own-layout – black

-1

多くのユーザーにとって、アラビア語のテキストは、同じではないにしても、同じ質問が複数回回答されたことを意味します。

私はちょうどここに私たちの共有の問題について検索するいくつかの時間を過ごした後

Android Arabic text aligment

How to support Arabic text in Android?

+0

あなたは彼の質問に答えておらず、これらの質問で使用されているレイアウト設定はPreferenceScreensには存在しません。 – matt5784

+0

Raul、これは別の問題です。テキストの配置は、何が求められているのか、右揃えかRightsレイアウトの右重力です。 –

+0

おっと!私の間違い。 –

11

をこれらを残しておきます、私は便利な何も見つかりませんでした。 obvioslyアンドロイドapiはRTL prefefrencesをサポートしていません。それはペルシア語/ヘブライ語/アラビア語の言語のサポートを意味しません。しかし、その後、私はeditTextを右揃えにする何らかの方法を見つけようとしました。私はEditTextPreferenceクラスを駆逐し、onCreateViewメソッドを再度実装しました。 は、ここに私のコードです:

/** 
* Created by omid on 1/12/14. 
*/ 
public class RtlTextPreference extends EditTextPreference { 


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

    @Override 
    protected View onCreateView(ViewGroup parent) { 
     View view = super.onCreateView(parent); 

     RelativeLayout layout = (RelativeLayout) ((LinearLayout) view).getChildAt(1); 
     layout.setGravity(Gravity.RIGHT); 
     return view; 
    } 
} 

標準のAndroid APIの好みの残りの部分は同じである必要があります(ところで、私はまだ試していません)。 おそらく、私たちはAndroidの設定でrtlサポートを完全に実装し、githubでホストする必要があります。 このコードスニペットが役に立ちますようお願いいたします。

+0

私はあなたの提案された解決策を試してきました、それは本当に私の環境設定のテキストを右に揃えますが、問題は、私のテキストの左側にある環境設定アイコンに対して同じことをしないということです。だから私の現在の外観は、テキストがスクリーンの最も右にあり、そのアイコンは画面の最も左にある。どのようにアイコンを整列させることができますか? –

+0

まだ試したことはありませんが、アイコンイメージ用の関連するImageViewへの参照を見つけたら、layout_alignToParentRightをtrueに設定するとうまくいくと思います。同じ名前のメソッドは直接持っていませんが、setLayoutParamsを使うことができます。 http://stackoverflow.com/questions/4305564/android-layout-right-align –

0

カスタムレイアウトを使用してsetLayoutResource(int layoutResId)を呼び出すことができます。

res/layout/preference_layout.xml:(これは、HoloEverywhereライブラリorg.holoeverywhere.preferenceに基づいています。カスタム優先して優先)

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="false" 
    android:gravity="center_vertical" 
    android:minHeight="?attr/listPreferredItemHeight" 
    android:paddingRight="@dimen/preference_item_padding_side" 
    android:paddingLeft="?android:attr/scrollbarSize" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:minWidth="@dimen/preference_icon_minWidth" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:minWidth="48dp" 
      android:contentDescription="@string/emptyString" 
      android:paddingRight="@dimen/preference_item_padding_inner" > 
     </ImageView> 
    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:paddingBottom="6dip" 
     android:gravity="right" 
     android:layout_gravity="right" 
     android:paddingLeft="@dimen/preference_item_padding_inner" 
     android:paddingTop="6dip" > 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="right" 
      android:singleLine="true" 
      android:textAppearance="?android:attr/textAppearanceMedium" > 
     </TextView> 

     <TextView 
      android:id="@+id/summary" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignRight="@id/title" 
      android:gravity="right" 
      android:layout_below="@id/title" 
      android:maxLines="10" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="?android:attr/textColorSecondary" > 
     </TextView> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/widget_frame" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:minWidth="@dimen/preference_widget_width" 
     android:orientation="vertical" > 
    </LinearLayout> 

</LinearLayout> 

:をチェックした後

5

マニフェストファイルでアプリケーションタグの下には、このライン、

アンドロイドを追加しますいくつかの解決策が私に役立つかもしれない何かに来た。 それはエレガントではありませんし、4でのみ動作し、最大しかし、それは何もないよりはましだ...

は(onResumeにこのコードを追加します)もちろん

Locale locale = new Locale("iw");// Hebrew in this case 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getResources().updateConfiguration(config, 
      getBaseContext().getResources().getDisplayMetrics()); 

あなたの好みの活動にロケールを変更することを選択できますMainActivityのonResume()メソッドのonResume()メソッドは、好みの(またはユーザが定義した)ロケールに戻します。

希望すると便利です。

+0

この属性はAPI 17以降でのみ使用されますが、良い点があります。 –

3

supportsRtl = "true" を:

@Override 
protected View onCreateView(ViewGroup parent) 
{ 
    setLayoutResource(R.layout.preference_layout); 

    return super.onCreateView(parent); 
} 
1
create a layout xml file called preference_checkbox.xml paste this (change the colour to what you want, only the gravity and the width set to fill parent are important) : 

<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"> 

<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="fill_parent" android:layout_height="wrap_content" android:gravity="right" 
     android:singleLine="true" android:textAppearance="?android:attr/textAppearanceMedium" 
     android:ellipsize="marquee" android:fadingEdge="horizontal" 
     android:textColor=#000000 /> 

    <TextView android:id="@+android:id/summary" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" 
     android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title" 
     android:textAppearance="?android:attr/textAppearanceSmall" android:textColor=#000000 
     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> 

and then in the preferences xml file for each row in the settings list that you want to right align add this: android:layout="@layout/preference_checkbox" 

thats it! 
got the solution from : http://stackoverflow.com/questions/7094584/checkboxpreference-with-own-layout 
the question there was about check box with image but it's the same technique. 
Good Luck. 
1

さてさて、これはかなり遅れるかもしれませんが、あなたは、Android 3.0以上をターゲットにしている場合は、とにかく、あなたは何とかRTL行うことが可能とPreferenceFragmentを使用する必要があります。

は、だから、このようなあなたのコード内のフラグメントを作成する必要があります。

import android.os.Bundle; 
import android.preference.PreferenceFragment; 

public class SettingsFragment extends PreferenceFragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     addPreferencesFromResource(R.xml.preferences); 
    } 
} 

は今、あなたがそのレイアウトにこのフラグメントを追加するアクティビティを作成する必要があります。 アクティビティ用のレイアウトを作成し、XML要素またはFragmentManagerクラスを使用してその中にフラグメントを配置できます。しかし、Right-To-Leftの設定を行うための鍵は、フラグメントの親要素のレイアウトファイルにlayout_direction = "rtl"属性を入れることです。すなわち

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    android:id="@+id/rootView" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layoutDirection="rtl" 
    android:orientation="vertical"> 

    <fragment 
     class="blah.blah.SettingsFragment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 

問題を修正RelativeLayout要素にandroid:layoutDirection="rtl"に注目してください。

他のアクティビティと同様にアクティビティJavaコードを追加し、マニフェストファイルに追加します。

あなたPreferenceFragment内側:

は手遅れ:)

2
public class RtlEditTextPreference extends EditTextPreference { 

@Override 
protected View onCreateView(ViewGroup parent) { 
    View view = super.onCreateView(parent); 

    RelativeLayout relativeLayout = (RelativeLayout)(((LinearLayout)view).getChildAt(1)); 
    relativeLayout.setGravity(Gravity.RIGHT); 

    TextView title = (TextView) relativeLayout.getChildAt(0); 
    TextView summary = (TextView) relativeLayout.getChildAt(1); 

    RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams)title.getLayoutParams(); 
    titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    title.setLayoutParams(titleLayoutParams); 

    RelativeLayout.LayoutParams summaryLayoutParams = (RelativeLayout.LayoutParams)summary.getLayoutParams(); 
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, title.getId()); 
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, -1); // Override default left alignment to the title 
    summary.setLayoutParams(summaryLayoutParams); 

    return view; 
} 

}

0
public class RtlEditTextPreference extends EditTextPreference { 
public RtlEditTextPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public RtlEditTextPreference(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

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

@Override 
protected View onCreateView(ViewGroup parent) { 
    View view = super.onCreateView(parent); 

    RelativeLayout relativeLayout = (RelativeLayout)(((LinearLayout)view).getChildAt(1)); 
    relativeLayout.setGravity(Gravity.RIGHT); 

    TextView title = (TextView) relativeLayout.getChildAt(0); 
    TextView summary = (TextView) relativeLayout.getChildAt(1); 

    RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams)title.getLayoutParams(); 
    titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    title.setLayoutParams(titleLayoutParams); 

    RelativeLayout.LayoutParams summaryLayoutParams = (RelativeLayout.LayoutParams)summary.getLayoutParams(); 
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, title.getId()); 
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, -1); // Override default left alignment to the title 
    summary.setLayoutParams(summaryLayoutParams); 

    return view; 
} 

}

0

私の問題と同じではありませんし、これが解決策であるホープ

@Override 
    public View onCreateView(LayoutInflater paramLayoutInflater, 
      ViewGroup paramViewGroup, Bundle paramBundle) { 
     getActivity().setTitle(R.string.fragment_title_settings); 
     View v = super.onCreateView(paramLayoutInflater, paramViewGroup, 
       paramBundle); 
     rtlView(v); 
     return v; 
    } 
    public void rtlView(View v){ 
     if(v instanceof ListView){ 
      rtllater((ListView) v); 
     } 
     else 
     if(v instanceof ViewGroup){ 
      if(v instanceof RelativeLayout){ 
       ((RelativeLayout)v).setGravity(Gravity.RIGHT); 
      } 
      else 
      if(v instanceof LinearLayout){ 
       ((LinearLayout)v).setGravity(Gravity.RIGHT); 
      } 
      for (int i=0;i<((ViewGroup)v).getChildCount();i++){ 
       rtlView(((ViewGroup)v).getChildAt(i)); 
      } 
     } 
     else 
      if(v instanceof TextView){ 
       v.getLayoutParams().width=v.getLayoutParams().MATCH_PARENT; 
       ((TextView)v).setGravity(Gravity.RIGHT); 
      } 
    } 
    public void rtllater(ListView v){ 
     v.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() { 
      @Override 
      public void onChildViewAdded(View view, View view1) { 
       rtlView(view1); 
      } 
      @Override 
      public void onChildViewRemoved(View view, View view1) { 

      } 
     }); 
    } 

結果は、この画像のようなものです:私の視点でright to left Preference

0

これが解決策になることができます: セットの各ビューのLayoutDirection RTLへ。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
View view = super.onCreateView(inflater, container, savedInstanceState); 

view.setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 

return view;} 
関連する問題