【问题标题】:How to create two line TextView on Android?如何在 Android 上创建两行 TextView?
【发布时间】:2017-04-27 13:41:34
【问题描述】:

我正在使用 Xamarin.forms 制作应用程序。

我的问题是无法创建两行 TextView。 即使我设置了足够的宽度,文本也会自动转到下一行。

如果我想创建单行,我将单行属性设置为 true 并将 setMaxLine 设置为 1。

但是这两条线,如果我同时设置 LineNumber 和 MaxLineNumber,它就不起作用。 文本将成为第三行。 例如,如果文本值为“我是婴儿而不是机器人。\n请给我一杯牛奶”,它就像

I'm a baby not robot.
Please give me 
a milk

如何解决这个问题?

谢谢。

【问题讨论】:

  • 尝试查看this希望对您有帮助
  • 我很抱歉。我正在尝试制作两行 TextView 而不是 EditText。我编辑了问题。
  • Xamarin 表单中没有 TextView。你能澄清一下吗?
  • @YuriS 是 XF 上的 Label,TextView 是 Android 端的原生 textview,不是吗?
  • 您可以分享演示问题的完整示例解决方案

标签: android xamarin xamarin.forms


【解决方案1】:

默认情况下,Android 中的所有EditText 小部件都是多行的。

这里是一些示例代码:

<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|left" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="fill_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>

【讨论】:

  • 我知道。但我想让它有两条线。但是如果我将 maxline 和 lines 设置为两个,它就不起作用了。
  • 即使使用 android:layout_height="wrap_content" ?
  • 我很抱歉。我正在尝试制作两行 TextView 而不是 EditText。我编辑了问题。
【解决方案2】:

这对我有用

<TextView
    android:text="I'm a baby not robot.\nPlease give me a milk"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:singleLine="false"
    android:maxLines="10" />

【讨论】:

    【解决方案3】:

    但是这两条线,如果我同时设置 LineNumber 和 MaxLineNumber,它就不起作用。文本将成为第三行。

    您可以创建自定义标签控件并在您的 Xaml 中使用它:

    自定义标签:

    public class CustomLabel:Label
    {
    }
    

    Xaml:

    <ContentPage ...
             xmlns:local="clr-namespace:LabelDemo"
             ...>
    <local:CustomLabel Text="I'm a baby not robot. \nPlease give me a milk"  VerticalOptions="Center" HorizontalOptions="Center" />
    

    然后在您的 Droid 项目中创建一个自定义渲染器,如下所示:

    using Android.Widget;
    using Xamarin.Forms.Platform.Android;
    using Xamarin.Forms;
    [assembly:ExportRenderer(typeof(LabelDemo.CustomLabel),typeof(LabelDemo.Droid.MyLabelRenderer))]
    namespace LabelDemo.Droid
    {
        public class MyLabelRenderer:LabelRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    var text = Control.Text;
                    text = text.Replace("\\n", "\n");//"\n" in shared project will be automatically transformed into "\\n", so we need to change it back
                    Control.SetLines(2);
                    Control.SetText(text, TextView.BufferType.Normal);
                }
            }
        }
    }
    

    【讨论】:

    • 很抱歉回复晚了,您的建议实际上是我在发布问题之前所做的。它不起作用,也许是因为我的情况有点不同。但实际上这并不能保证自动剪切文本。我的文本将显示到第二行,但问题是第二行实际上不是第二行,而是自动从第一行剪切。
    • 那我猜可能是布局问题。在空白项目上,它工作正常。你能分享一些代码来展示你的作品吗?
    • 我认为我应该这样做。但我很抱歉。我想这是你所说的布局问题,不像如何使用自定义渲染器。我想我应该使用不同的方法。谢谢你们到目前为止。我的代码有点复杂,有时间我会展示一下。
    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多