TextView within a TableLayout only centered after configuration change
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="in开发者_如何学Pythonvisible"
>
<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>
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.
精彩评论