Android 更新文本视图和对齐方式

发布于 2024-11-14 21:24:46 字数 1360 浏览 10 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻了热茶 2024-11-21 21:24:46

将 android:gravity="right" 提及到 android:id="@+id/spaceValue"。示例布局并经过测试效果很好。

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TableLayout android:layout_gravity="center_horizontal"
    android:stretchColumns="*">
    <TableRow>
        <TextView android:id="@+id/spaceValue" android:text="3000"
            android:textStyle="bold" android:textColor="#FFFFFF"
            android:layout_gravity="right"  android: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>

Mention android:gravity="right" to android:id="@+id/spaceValue". A sample layout and tested works fine.

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TableLayout android:layout_gravity="center_horizontal"
    android:stretchColumns="*">
    <TableRow>
        <TextView android:id="@+id/spaceValue" android:text="3000"
            android:textStyle="bold" android:textColor="#FFFFFF"
            android:layout_gravity="right"  android: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>
空‖城人不在 2024-11-21 21:24:46

将代码重新格式化为
后,

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:layout_gravity="center_horizontal"
android:stretchColumns="*" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TableRow>
        <TextView android:id="@+id/spaceValue" android:text="4"
            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>

4 米和 30000 米看起来都是一样的。

After reformatting your code to

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:layout_gravity="center_horizontal"
android:stretchColumns="*" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TableRow>
        <TextView android:id="@+id/spaceValue" android:text="4"
            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>

both 4meters and 30000meters looks the same way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文