我有一个 Textview,我需要在其中以正确的格式显示大量信息。我怎样才能实现它?
我想这样显示:
Title : Some Title.
Date : Date
More Info about the title.
......
......
Contact : Contact Details.
那么你必须通过计算选项之间的空间来做到这一点。
更好的方法
使用Webview
使用 html 格式正确对齐内容的 html 字符串并加载 webview 并显示它。
如果使用 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>
我认为有三种选择。
正如其他人所建议的那样,WebView 是您的选择。请参阅 如何以编程方式设置/编辑 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.");