【问题标题】:How to Align the Text Properly in TextView?如何在 TextView 中正确对齐文本?
【发布时间】:2013-02-11 22:31:28
【问题描述】:

我有一个 Textview,我需要在其中以适当的格式显示大量信息。我该如何实现它?

我想这样显示:

Title  :       Some Title.
Date   :       Date

More Info about the title.
......
......

Contact :      Contact Details.

【问题讨论】:

    标签: android textview alignment


    【解决方案1】:

    你必须通过计算选项之间的空间来做到这一点。

    更好的方法

    使用Webview 使用 html 格式制作内容正确对齐的 html 字符串,然后加载 webview 并显示它。

    【讨论】:

    • 我想你说的是iOS功能。
    • 哎呀我是......但它也适用于android
    • 设置 webview 和 html 背景的背景颜色以清除颜色
    • "background-color: transparent" 会在 html 内容背景中做到透明。其他你可以通过代码做到
    • 一些用户对此有疑问。你可以在这里看到:stackoverflow.com/questions/5003156/…
    【解决方案2】:

    如果使用 TextView 不是必需的,您可以使用 TableLayout:

    <TableLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow>
            <TextView 
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Title"
                android:layout_weight="1"/>
    
            <TextView 
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Some Title"
                android:layout_weight="2"/>
        </TableRow>
    
        <TableRow>
            <TextView 
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Date"
                android:layout_weight="1"/>
    
            <TextView 
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Some Date"
                android:layout_weight="2"/>
        </TableRow>
    
    </TableLayout>
    

    【讨论】:

      【解决方案3】:

      我认为有三种选择。

      1. 如果字体无关紧要,请在 textview 中使用 android:typeface="monospace" 并通过空格对齐
      2. 如果字体确实重要,请在 html 中设计并使用 WebView
      3. 如果您不想使用 html,则需要通过不同的 TextView 进行布局,这总是更好。

      【讨论】:

        【解决方案4】:

        正如其他人所建议的,WebView 是您的选择。看 How to programmatically set / edit content of webview

        但是,如果文本不需要特殊格式(例如不同的颜色、字体大小)并且唯一需要的格式是缩进,则可以在 TextView 中执行此操作。

        在布局中:

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false" />
        

        活动中:

            TextView tv = (TextView) findViewById(R.id.text);
            tv.setText("Title  :       Some Title.\nDate  :       Date\n\nMore Info about the title.\n......\n......\n\nContact :      Contact Details.");
        

        【讨论】:

          猜你喜欢
          • 2018-07-31
          • 1970-01-01
          • 2020-09-06
          • 1970-01-01
          • 2013-03-18
          • 2014-02-06
          • 2014-11-03
          • 1970-01-01
          相关资源
          最近更新 更多