【问题标题】:How to set text inside TextView to be in two lines?如何将 TextView 中的文本设置为两行?
【发布时间】:2011-12-09 03:52:16
【问题描述】:

有没有办法以编程方式将TextView 中的文本设置为两行?

【问题讨论】:

    标签: android android-widget textview


    【解决方案1】:

    输入\n 换行

    This will be\ntwo lines
    

    看起来像

    This will be
    two lines
    

    另外,请确保 TextViews setsingleline() 未设置为 true

    【讨论】:

    • 还要检查您的 XML 中没有 android:maxLines 限制。
    【解决方案2】:
    TextView tv = new TextView(this);
    tv.setText("First Line \n this is Second Line");
    setContentView(tv);
    

    【讨论】:

      【解决方案3】:

      您可以使用\n 在您的文本视图中插入换行符。

      像这样:

      playing.setText( "STATION: \n" + stations.get( position ) );
      

      【讨论】:

        【解决方案4】:

        确保您的 TextView 在 xml 中有几行代码:

        android:lines="2" android:singleLine="false"

        默认情况下 android:singleLine 为 true。所以你可以在 XML 或 Java 代码中设置它,即:

        txtNextLine.setSingleLine(false);
        txtNotActivate.setText("first line\n"+"second line");
        

        希望这会有所帮助。

        【讨论】:

        • singleLine 已弃用,请使用 maxLine = "1"
        猜你喜欢
        • 1970-01-01
        • 2014-12-09
        • 2013-10-27
        • 2015-12-05
        • 1970-01-01
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多