首页 > 解决方案 > 如何强制 Android 对话框中的文本向左对齐

问题描述

 var dialogView = LayoutInflater.Inflate(Resource.Layout.list_view, null);
        Android.App.AlertDialog alertDialog;

        listview = dialogView.FindViewById<ListView>(Resource.Id.listview);

        var items = new string[] { "English", "español", "中文(s)", "中文(t)", "日本語", "हिन्दी", 
        "français", "Deutsche", "русский", "عربى" };
        //  var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, 
        items);
        var adapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, items);

显示列表时没有阿拉伯语,所有项目都向左对齐。但是,由于阿拉伯语是从右到左的语言,因此最终条目(在阿拉伯语中为阿拉伯语)显示在右侧。我想把它放在左边,这样它就和其他的对齐了。

我该怎么做?重力?

标签: androidxamarinandroid-alertdialog

解决方案


您可以android:gravity属性来定义内容的左对齐

喜欢list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:textColor="#f00"
   android:gravity="left"
   > 

</TextView>

或者使用LTRAndroid 4.2 (sdk 17) 的布局

android:supportsRtl="true"在你的定义AndroidManifest.xml

<application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">

</application>

然后android:textDirection="ltr"在你的定义list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:textDirection="ltr"
   android:textColor="#f00"
   >
</TextView>

推荐阅读