我在textview文字对齐方面遇到问题

时间:2019-09-19 13:14:36

标签: c# xamarin.android visual-studio-2019

我正在文本视图中显示我的OCR文本,但是文本视图中的某些行没有达到屏幕宽度。但是某些行却到达了末尾。我不知道该怎么做,请帮帮我。< / p>

如果您在文本视图中看到我的输出,我仍然有一个空格来扩展我的行,但是它显示为新行

我的文本视图输出

It is with great enthusiasm that I accept the Foreign 
Service Officer position with the
Department of State. 
I feel confident that I can make a significant contribution to the
agency. 
  

xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff006064">
    <Button
        android:text="Pick Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/MyButton" />
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="match_parent"
        android:layout_height="295.0dp"
        android:id="@+id/imageView1" />
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txtView"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:text="No Text"
            android:scrollbars="vertical|horizontal"
            android:textStyle="bold"
            android:textColor="#ffffffff" 
            android:textIsSelectable="true"
            android:layout_marginLeft="25dp"
            android:layout_weight="1"
            android:ellipsize="end"/>
</ScrollView>
</LinearLayout>
  

.cs文件

TextRecognizer txtRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();
                if (!txtRecognizer.IsOperational)
                {
                    Log.Error("Error", "Detector dependencies are not yet available");
                }
                else
                {
                    Frame frame = new Frame.Builder().SetBitmap(bitmap).Build();
                    SparseArray items = txtRecognizer.Detect(frame);
                    StringBuilder strBuilder = new StringBuilder();
                    for (int i = 0; i < items.Size(); i++)
                    {
                        TextBlock item = (TextBlock)items.ValueAt(i);
                        strBuilder.Append(item.Value);
                        strBuilder.Append("/n");
                    }

                    int myLimit = 8;
                    string sentence = strBuilder.ToString();
                    string[] words = sentence.Split("/n",' ');

                    StringBuilder newSentence = new StringBuilder();

                    string line =".";
                    foreach (string word in words)
                    {
                        if ((line + word).Length > myLimit)
                        {
                            newSentence.AppendLine(line);
                            line = " ";
                        }

                        line += string.Format("{0} ", word);
                    }

                    if (line.Length > 0)
                        newSentence.AppendLine(line);

                    txtView.Text= newSentence.ToString();


                }

            }

1 个答案:

答案 0 :(得分:0)

更改

 android:layout_width="350dp"

android:layout_width="match_parent"

编辑:根据您的评论,看来问题出在C#代码中,OCR中的原始字符串可能包含“ \ r \ n”或“ \ r”换行符,而且您只在寻找“ \ n”换行符。

所以,我会改变

string[] words = sentence.Split("/n", ' ');

string[] words = sentence.Split(new[] { " ", "\n", "\r\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);

看看是否有帮助。