2012-06-07 83 views
0

我有一個XML佈局,其中包含谷歌地圖,兩個文本視圖和一個相對佈局中的按鈕。當用戶在地圖上移動光標時,文本視圖會在谷歌地圖下方顯示文本值「緯度:」和「經度:」,並更新爲「緯度:xxx」和「經度」xxx「。 「提交」就是這樣,它打包緯度/經度,並將它傳遞給下一個活動。Android XML佈局錯誤

這一切都很好,直到我開始搞亂佈局。我注意到因爲我懶洋洋地使用在Eclipse中的圖形設計工具,一切都設置了尺寸,在更大的屏幕上的意思是它看起來很愚蠢

我添加了另一個相對佈局,並將緯度/長度文本和提交按鈕放入它,相對佈局,現在只有MapView。

我的意圖是使用android:layout_weight爲底部的15%左右的屏幕分配底部相對佈局,屏幕的其餘部分將是谷歌地圖。

物理上尺寸很好。但是,如果我的文本視圖依賴於提交按鈕(android:layout_above/below),則提交按鈕的文本將取決於文本視圖的值,並會更新,而textview會卡在「lat/long:「並且不會更新。

我目前的textviews只是設置在相對佈局的頂部和底部的獨立提交按鈕,它工作正常。我只是好奇到底發生了什麼。

這裏是我的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="80" > 

    <com.google.android.maps.MapView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mapView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:apiKey="xxx" 
     android:clickable="true" 
     android:enabled="true" > 
    </com.google.android.maps.MapView> 
</RelativeLayout> 


<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="20" > 

    <TextView 
     android:id="@+id/Latitude" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Latitude:" /> 

    <TextView 
     android:id="@+id/Longitude" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/Latitude" 
     android:text="Longitude:" /> 

    <Button 
     android:id="@+id/submitbutton" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:onClick="GOTOUPLOADER" 
     android:text="Submit" /> 

</RelativeLayout> 

而且這裏是唯一的(我認爲)相關的代碼與此xml文件的工作原理。請注意,我不在這裏使用按鈕文字做任何事情。這甚至不是這個班的一個屬性。按鈕與這個類的唯一交互是它調用一個方法來在單擊時啓動另一個活動。

//setupmap()instantiates mapview and centers on the given location 
public void setupmap() 
    { 
     getcoords();//setup coordinates 
     lattext.setText("Latitude: "+String.valueOf(Lat)); 
     lontext.setText("Longitude: "+String.valueOf(Long)); 
     mapView = (MapView) findViewById(R.id.mapView);//instantiate mapview 
     mapView.setBuiltInZoomControls(true);//able to zoom 
     mcontrol = mapView.getController();//mapcontroller contains methods to manipulate mapview 
     mcontrol.animateTo(point);//zoom to our geopoint 
     mcontrol.setZoom(19);//zoom level 1-21. 1=world view, 21=as zoomed in as possible 
     MyOverlay over = new MyOverlay();//instantiate overlay 
     listofoverlays = mapView.getOverlays();//create a list of overlays 
     listofoverlays.clear();//clear list 
     listofoverlays.add(over);//add our overlay 
     mapView.invalidate();//invalidate for changes to take effect 
    }//setupmap 

請原諒所有評論。

+0

我不理解5º段落。你的問題是TextViews沒有被更新?併發布與問題的XML。 –

+0

我相信這是不相關的,但你錯過了。另外,MapView'android:enabled =「true」'已棄用,如果您將textview的方向之一更改爲依賴於提交按鈕,則使用'android:state_enabled =「true」' – jnthnjns

+0

@AlexOliveira,提交按鈕的文本變爲依賴文本視圖的值並將不再調用android:onclick指定的「GOTOUPLOADER」方法。因此,如果我使用android:layout_below =「submitbutton」在submitbutton下面的緯度textview,那麼submitbutton的文本屬性將變爲42.76485或者任何緯度值,並且點擊它將什麼都不做。 – 0nyx

回答

1

好吧,我仍然不肯定我理解你的問題完全,但開始嘗試解決此問題:

我在這裏做的是採取了你的代碼,並放置關係定位回來就可以了。請注意,如果您將一個對象與另一個對象關聯,則必須將要引用的對象放置在該對象的下方。這是由於id不在生成的R.java中,直到它被android:[email protected]+id/object聲明爲止。在這種情況下,如果你想聲明一個對象爲above另一個,那麼我們從底層開始構建。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="80" > 

     <com.google.android.maps.MapView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/mapView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:apiKey="xxx" 
      android:clickable="true" 
      android:enabled="true" > 
     </com.google.android.maps.MapView> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" > 

     <Button 
      android:id="@+id/submitbutton" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:text="Submit" /> 

     <TextView 
      android:id="@+id/Longitude" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_above="@id/submitbutton" 
      android:text="Longitude:" /> 

     <TextView 
      android:id="@+id/Latitude" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_above="@id/Longitude" 
      android:layout_alignParentLeft="true" 
      android:text="Latitude:" /> 
    </RelativeLayout> 

</LinearLayout> 

編輯:

你的榜樣來看,你的問題似乎是在你的java。你打電話TextView如何使用setText你顯示?

見下面的例子:

TextView lattext = (TextView)findViewById(R.id.Latitude); 
lattext.setText("Latitude: " + String.valueOf(Lat)); 

// And 

TextView lontext= (TextView)findViewById(R.id.Longitude); 
// Avoid using "Long", Long is reserved... If I am unsure of whether text is reserved 
// I will always place my initials in front of it... i.e. "String.valueOf(jjLong));" 
lontext.setText("Latitude: " + String.valueOf(Lon)); 
+0

我修改了我的xml文檔以匹配你所做的,而且我仍然收到錯誤。我從模擬器上截取了一些截圖來說明我所看到的。 NOBUG-HTTP://imageshack.us/photo/my-images/835/righta.png/。 BUG-http://imageshack.us/photo/my-images/259/wrongu.png/ – 0nyx

+0

請參閱上面的編輯回答。 – jnthnjns