TableLayout 中的 TextView 仅在配置更改后居中
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我无法重现您的错误,我建议您尝试不同的布局(具有相同的假定结果显示方面),例如:
希望它能解决您的 cy 7.0.1 上的问题。
更新
关于您的代码的一些建议:
在
MyLocationListener
中,您不应该每次都设置两个布局的可见性,这样会更有效:另外,在
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:
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: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.