2011-05-02 63 views
2

在我main.xml中,我有了2列和2行的表;一個緯度爲&經度及其相關值。我希望它們在它們的列中居中,從我的閱讀中看來,最好的方法是使用layout_weight =「1」和gravity =「center」。這確實工作的時間,但因爲我已經在代碼的其他地方所做的更改,而現在沒有人在gps_layout的項目都集中除非的配置發生變化,在這種情況下,當我通過旋轉手機改變方向。我知道輪換會導致應用程序經歷其生命週期(除了調用重新生成之外,還有其他簡單的說法),但我不明白第一次運行後應用程序的不同之處,除了它的保存狀態......這對中心定位似乎沒有太大影響。謝謝!對不起,如果我沒有正確地格式化這個問題,我已經拖了一段時間,但這是我的第一個問題。的TextView只配置更改後中心

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

您是否應用過可能導致此問題的任何主題/風格?或者在你的佈局開始搞亂之後,代碼中有什麼變化?您發佈的佈局正在按預期工作(顯示)。 – rekaszeru 2011-05-03 05:51:26

+0

我還沒有設置任何主題/樣式,我在ScrollView內的RelativeLayout中有該TableLayout。至於對代碼的更改......我正在處理一些其他看似無關的問題,這些問題涉及我的主要java類,其中包括gps被禁用,因此我可以更快地運行程序以進行調試,當gps被禁用時,桌面gps_layout是不可見的,所以我不知道這是什麼時候發生的:(我很樂意與你分享整個代碼,但那可能比我可以問的人更努力) – 2011-05-03 19:47:15

+0

如果你可以臨時分享它,會很棒,我會倒是喜歡看看它(上午)請記住,您的問題誰花時間來回答大家的挑戰,而且在大多數情況下,也學到一些東西。所以,即使努力,感謝 – rekaszeru 2011-05-03 20:30:08

回答

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)的知名度,你也應該設置爲其他爲好。

+0

.. hrm首先是一個fc,因爲我忘了將我的類中的類型從TableLayout更改爲LinearLayout ...現在是fc,因爲...?加我複製粘貼你的代碼...不幸的是,我只知道如何使用調試器。 – 2011-05-04 18:46:42

+0

啊,真是太棒了!我之前就想這麼做,因爲我知道重複做這件事效率低下,但我試圖解決這個問題。我不知道正確的代碼...現在調試器似乎告訴我,gps_layout內的linearlayout需要一個寬度,它似乎不會因爲它有一個重量... – 2011-05-04 18:55:34

+0

好的,真棒代碼工作和它的中心現在,但我不得不添加'android:layout_width =「wrap_content」'兩個內部線性佈局。我認爲重量照顧寬度?有任何想法嗎?仍然很好奇爲什麼我的手機發現反對中心開始。 – 2011-05-04 19:02:19