2010-11-04 31 views
0

我正試圖在我的主視圖上顯示一個新視圖。Android查看未出現

這裏是新的視圖中的XML:

<RelativeLayout 
     android:id="@+id/mapdetaillayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="left" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 

      <TextView 
       android:text="This is the Text from the XML File." 
       android:id="@+id/DetailTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

    </RelativeLayout>  

這裏是我使用推新viev到屏幕上的代碼:

 RelativeLayout DetailLayout = new RelativeLayout(c); 

     LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.mapdetailview, DetailLayout);  

     TextView tv = (TextView) v.findViewById(R.id.DetailTextView); 
     tv.setText("HERE IS THE TEXT FROM THE CODE"); 

     // This Log works 
     Log.i("MESSAGE", tv.getText()); 

     DetailLayout.setBackgroundColor(Color.WHITE); 

     LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     params.gravity = Gravity.NO_GRAVITY; 

     DetailLayout.bringToFront(); 
     DetailLayout.setVisibility(View.VISIBLE); 

代碼被調用,Log輸出預期的文本,這對我來說意味着View已經被創建 - 它只是不顯示在屏幕上。

任何幫助,將不勝感激。

回答

3

我在代碼中看不到任何對Activity的setContentView()方法的調用。可能你只是不這樣做?如果是這樣,那就是你什麼都看不到的原因。你也不需要手動使用RelativeLayout。嘗試只指定佈局資源爲內容的看法:

setContentView(R.layout.mapdetailview); 

然後,您可以只是讓你的TextView:

TextView tv = (TextView) findViewById(R.id.DetailTextView); 

我希望幫助。

+0

爲什麼你這樣做?看起來對我來說完全錯了。 – 2010-11-05 02:56:14

0

您是否嘗試將LayoutParams.WRAP_CONTENT更改爲fill_parent以查看是否有任何更改?