2012-07-19 89 views
3

我最近開始使用Android。我沒有那麼多Android的經驗。這是我第一次使用Android。我需要將屏幕分成兩半,然後執行幾項任務。將Android屏幕分成兩半等分

  1. 劃分屏幕在兩個相等的半,在第二半之後(意味着下半部)我需要一個名爲Latitude然後對應於該一個Textbox標籤,將顯示當前的緯度值,並命名爲Longitude另一個標籤和對應於另一個Textbox並且將顯示當前的經度值。

我開始研究這個,並且我取得了一些進展 - 下面是我正在使用的XML文件將屏幕分成兩半。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.8" 
     android:background="#000000" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#000000" > 

     <!-- Latitude Label --> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Latitude" 
      android:textColor="#372c24" /> 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="20dip" 
      android:layout_marginTop="5dip" 
      android:singleLine="true" /> 
     <!-- Longitude Label --> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Longitude" 
      android:textColor="#372c24" /> 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dip" 
      android:password="true" 
      android:singleLine="true" /> 
    </LinearLayout> 

</LinearLayout> 

我的問題是 -

  1. 使用上述XML文件後,我無法看到我的both labelboth textbox正確對Android屏幕,有什麼我很想念在我的XML文件?其次,我如何在相應的文本框中顯示當前的緯度和經度值。

任何幫助將不勝感激,因爲我是Android世界的新手。

更新: - 下面是我在油漆中做的圖,我想要這樣的東西。 enter image description here

如果你看看屏幕,它分爲兩部分,在下半部分有兩個標籤(經度和緯度),它應該是這樣的,每個標籤的前面,都會有是一個文本框,它將保存相應的緯度和經度值。

更新代碼: -對於當前的當前位置 -

public class MainActivity extends Activity implements LocationListener { 
    private TextView latituteField; 
    private TextView longitudeField; 
    private LocationManager locationManager; 
    private String provider; 

    private static final String TAG = "CurrentLocation"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     latituteField = (TextView) findViewById(R.id.lat1); 
     longitudeField = (TextView) findViewById(R.id.lat2); 

    // Get the location manager 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the location provider -> use 
     // default 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     locationManager.requestLocationUpdates(provider, 0, 0, this); 
     Location location = locationManager.getLastKnownLocation(provider); 
     onLocationChanged(location); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     locationManager.removeUpdates(this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      System.out.println("Provider " + provider + " has been selected."); 
      int lat = (int) (location.getLatitude()); 
      int lng = (int) (location.getLongitude()); 
      Log.v(TAG, lat+ " "+ lng); 
      latituteField.setText(String.valueOf(lat)); 
      longitudeField.setText(String.valueOf(lng)); 
     } else { 
      latituteField.setText("Provider not available"); 
      longitudeField.setText("Provider not available"); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 
} 

但是,下面的代碼沒有顯示在對應的文本框中輸入任何經緯度值?我在做什麼錯?

+0

哎呀..對不起,我忘了接受答案。我現在做了.. – AKIWEB 2012-08-02 04:34:56

回答

3

「使用上面的XML文件後,我沒能看到我的標籤和 兩個文本框正確對Android屏幕,有什麼我 失蹤在我的XML文件?」

它看起來,你可能有你的LinearLayout標籤搞砸了。

看看這是更接近你正在尋找:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <!-- currently empty --> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <!-- Latitude Label --> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Latitude" 
       android:textColor="#372c24" /> 

      <EditText 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="20dip" 
       android:layout_marginTop="5dip" 
       android:layout_weight="0.35" 
       android:singleLine="true" 
       android:text="test1" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <!-- Longitude Label --> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Longitude" 
       android:textColor="#372c24" /> 

      <EditText 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dip" 
       android:layout_weight="0.35" 
       android:password="true" 
       android:singleLine="true" 
       android:text="test2" /> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

圖片

enter image description here


「其次,我怎麼能顯示當前的緯度和經度值在 對應的文本框。「

你有什麼企圖?

您需要獲取當前的緯度/經度。 然後只需在EditText上設置文字。

+0

感謝prolink建議,但這不是我想要的方式。目前,我在頂部獲得了經度和緯度,並且這兩個標籤都來自對方的前面,我也看不到相應的文本框。而且我需要第二個線性佈局的頁面下半部分的標籤和相應的文本框。 – AKIWEB 2012-07-19 04:13:35

+0

你可以畫一個你想要的簡單圖片併發布在這裏嗎?這會讓每個人都更容易理解。 – prolink007 2012-07-19 04:15:21

+0

我將它更改爲垂直'LinearLayout',看看你是否在談論這個。 – prolink007 2012-07-19 04:17:02