Android 更新文本视图和对齐方式
我有 2 个并排的文本视图,一个右对齐,一个左对齐,以将它们保持在一起。右侧只是左侧给出的测量类型的标签。问题是,当左边的TextView数量很大时,两个TextView是并排的,中间没有间隙。但是一旦左侧的 TextView 变小,它就会与右侧间隔开。
例如,在一个周期内它可能看起来像这样: 3000米
然后这样就下一个: 5 米,
我需要它来将它们保持在一起。更新视图时,我可以在 xml 端还是代码端做些什么来实现此目的?
编辑 - 好吧,我必须在写字板中编辑它才能让它在这里正确显示
<TableRow android:layout_width="fill_parent"
android:background="@drawable/maindatabg">
<TableLayout
android:layout_gravity="center_horizontal"
android:stretchColumns="*">
<TableRow>
<TextView
android:id="@+id/spaceValue"
android:text="00.0"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_gravity="right"
android:textSize="20dip"/>
<TextView
android:id="@+id/spaceTypeValue"
android:layout_height="fill_parent"
android:text="meters"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_gravity="left"
android:textSize="14dip"/>
</TableRow>
</TableLayout>
</TableRow>
I have 2 textviews side by side, one aligned right and one left to keep them together. The right side is simply a label for the type of measurement given in the left side. The problem is that when the left TextView is a large number, the two TextView's are side-by-side with no gap in the middle. But as soon as the left TextView becomes a small number, it becomes spaced apart from the right side.
For example, it might look like this during one cycle:
3000meters
And then like this on the next:
5 meters
I need it to keep them together. Is there something I can do to achieve this, xml side or code side when updating the view?
edit - okay I had to edit it in wordpad to get it to show correctly in here
<TableRow android:layout_width="fill_parent"
android:background="@drawable/maindatabg">
<TableLayout
android:layout_gravity="center_horizontal"
android:stretchColumns="*">
<TableRow>
<TextView
android:id="@+id/spaceValue"
android:text="00.0"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_gravity="right"
android:textSize="20dip"/>
<TextView
android:id="@+id/spaceTypeValue"
android:layout_height="fill_parent"
android:text="meters"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_gravity="left"
android:textSize="14dip"/>
</TableRow>
</TableLayout>
</TableRow>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 android:gravity="right" 提及到 android:id="@+id/spaceValue"。示例布局并经过测试效果很好。
Mention android:gravity="right" to android:id="@+id/spaceValue". A sample layout and tested works fine.
将代码重新格式化为
后,
4 米和 30000 米看起来都是一样的。
After reformatting your code to
both 4meters and 30000meters looks the same way.