TableLayout 中的 TextView 仅在配置更改后居中

发布于 2024-11-05 00:18:20 字数 1083 浏览 7 评论 0原文

在我的 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>

Within my main.xml, I have a table that has 2 columns and 2 rows; one for latitude & longitude and their associated values. I want them to be centered within their columns, and from my readings it seemed the best way to do that is with layout_weight="1" and gravity="center". This did work most of the time, but since I have made changes elsewhere in the code, now none of the items in gps_layout are centered unless the configuration is changed, in this case when I change the orientation by rotating the phone.

I know that rotation causes the app to go through its lifecycle (also is there a shorthand for talking about that other than calling it rebirth?) but I cannot understand what is different about the app after the first run, except for its saved state ... which would not seem to have much effect on centering. Thanks! Sorry if I did not format this question correctly, I have been trolling SO for awhile but this is my first question.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

萌酱 2024-11-12 00:18:20

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

<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) 可见性的任何地方,您还应该设置另一种布局。

Since I couldn't reproduce your error, I'd suggest you give a try to a different layout (with the same supposed resulting display aspect), like:

<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>

Hopefully it will solve the problem on your cy 7.0.1.

Update
Some advises regarding your code:

In the MyLocationListener you shouldn't set every time the visibility of the two layout, this way it would be more effective:

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

also, in onProviderEnabled method, and everywhere where you set the visibility of one layout (acquire_view/gps_view), you should also set for the other as well.

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