【问题标题】:TextView within a TableLayout only centered after configuration changeTableLayout 中的 TextView 仅在配置更改后居中
【发布时间】:2023-03-17 19:56:01
【问题描述】:

在我的 main.xml 中,我有一个有 2 列和 2 行的表;一个用于纬度和经度及其相关值。我希望它们在它们的列中居中,从我的阅读来看,最好的方法似乎是使用 layout_weight="1" 和gravity="center"。这在大多数时间确实有效,但是由于我在代码的其他地方进行了更改,现在 gps_layout 中的所有项目都没有居中除非配置被更改,在这个当我通过旋转手机改变方向时的情况。

我知道轮换会导致应用程序经历其生命周期(除了称其为重生之外,还有没有一种简写方式来谈论它?)但我不明白第一次运行后应用程序有什么不同,除了它保存状态......这似乎对居中没有太大影响。谢谢!抱歉,如果我没有正确格式化这个问题,我已经拖了一段时间了,但这是我的第一个问题。

<TableLayout
    android:id="@+id/gps_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    >
    <TableRow>
        <TextView
        android:layout_column="1"
        android:id="@+id/lat_text"
        android:text="latitude: "
        android:layout_weight="1"
        android:gravity="center"                
        />
        <TextView    
        android:id="@+id/lon_text"
        android:text="longitude: "
        android:layout_weight="1"
        android:gravity="center"
        />
    </TableRow>
    <TableRow>
    ...etc...
    </TableRow>
</TableLayout>

【问题讨论】:

  • 您是否应用了任何可能导致此问题的主题/样式?或者在您的布局开始混乱之后代码发生了哪些变化?您发布的布局正在按预期工作(显示)。
  • 我还没有设置任何主题/样式,我确实在 ScrollView 中的 RelativeLayout 中有那个 TableLayout。至于对代码的更改...我正在处理与我的主要 Java 类相关的许多其他看似无关的问题,这些问题涉及禁用 gps,因此我可以更快地运行程序以进行调试,当 gps 被禁用时,表 gps_layout 是不可见的,所以我不知道这是什么时候发生的 :( 我很想和你分享整个代码,但这可能比我要求别人付出的更多。
  • 如果你能在某个地方暂时分享它,那就太好了,我很想看看它(早上)。请记住,对于您的问题,每个花时间回答的人都会受到挑战,并且在大多数情况下也会学到一些东西;所以即使努力,谢谢。
  • 太棒了!好吧,我要开一个 github 帐户,我看到那里发布了很多代码。让我知道是否有更好的地方我应该尝试,或者将我的整个项目的公共保管箱链接发送给您是否更容易,这样您就可以在您的机器上打开它而无需复制和粘贴。否则,请稍后在此处发布代码链接。
  • 好吧,我决定反对 github,这是我的 Dropbox 文件夹的公共链接,里面包含所有文件。忽略apk,它已经过时了。另外,如果代码很幼稚,请提前道歉,这是我编写的第一个应用程序,多年来我没有上过 Java 课程。 (dl.dropbox.com/u/16042321/Marine%20Debris.zip) 再次,非常感谢!!!

标签: android android-layout centering


【解决方案1】:

由于我无法重现您的错误,我建议您尝试不同的布局(具有相同的假定结果显示方面),例如:

<LinearLayout android:id="@+id/gps_layout" android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentTop="true" android:layout_alignBottom="@id/acquire"
    android:visibility="invisible">
    <LinearLayout android:orientation="vertical" android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/lat_text"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="latitude: " 
            android:gravity="center" />
        <TextView android:id="@+id/lat_data"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="(lat goes here)" 
            android:gravity="center" />
    </LinearLayout>
    <LinearLayout android:orientation="vertical" android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/lon_text"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="longitude: " android:gravity="center" />
        <TextView android:id="@+id/lon_data"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="(lon goes here)" 
            android:gravity="center" />
    </LinearLayout>
</LinearLayout>

希望它能解决您的 cy 7.0.1 上的问题。

更新
关于您的代码的一些建议:

MyLocationListener中不要每次都设置两个布局的可见性,这样会更有效:

@Override
public void onLocationChanged(Location loc)
{
    if (acquire_view.getVisibility() == View.VISIBLE)
    {
        acquire_view.setVisibility(View.INVISIBLE);
        gps_view.setVisibility(View.VISIBLE);
    }
}

另外,在 onProviderEnabled 方法中,以及在任何地方设置一个布局的可见性 (acquire_view/gps_view),您也应该为另一个设置。

【讨论】:

  • .. hrm 首先是一个 fc,因为我忘记将我的类中的类型从 TableLayout 更改为 LinearLayout ...现在是一个 fc,因为 ...?哎呀,我复制粘贴了你的代码......不幸的是,我只知道如何使用调试器。
  • 啊,太好了!我早些时候试图这样做,因为我知道重复这样做是低效的,但我在试图解决这个问题时陷入了困境。而且我不太了解正确的代码......现在调试器似乎告诉我 gps_layout 中的线性布局需要一个宽度,因为它有一个重量,所以它似乎不需要......
  • 好的,你的代码工作得很好,它现在居中,但我必须在两个内部线性布局中添加android:layout_width="wrap_content"。我认为重量照顾宽度?有任何想法吗?仍然很好奇为什么我的手机一开始就反对居中。
  • 我真的很高兴,你成功了!请告诉我进展如何
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多