2016-10-18 79 views
0

的TextView我一直在試圖增加谷歌的片段下方的LinearLayout裏面一個TextView編程映射,但文中觀點似乎沒有得到補充。這裏是我的xml文件:添加以下地圖碎片動態

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:id="@+id/root"> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    tools:context="com.example.burhan.googlemapsdemoapp.MapsActivity" /> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    android:gravity="center_horizontal" 
    android:id="@+id/mapLayout"> 


</LinearLayout> 

,這裏是我的,用於將文本視圖代碼:

linearLayout = (LinearLayout) findViewById(R.id.mapLayout);  
final TextView tv = new TextView(Map.this); 

tv.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); 
tv.setHeight(LinearLayout.LayoutParams.MATCH_PARENT); 

tv.setText("Some text"); 
tv.setGravity(Gravity.CENTER); 

Log.d("View","StackTrace"); 

try{ 
    linearLayout.addView(tv); 
}catch (Exception e){ 
    Log.e("MYAPP", "exception", e); 
} 
+0

嘗試調用根的LinearLayout requestlayout()後您添加的TextView – MidasLefko

+0

爲什麼try/catch塊? – egoldx

+0

正試圖找出是否有任何例外......完全沒有挫折! @ user1779222 –

回答

0

更改您的佈局,這一點,並刪除你的動態TextView中添加。

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="1" 
android:id="@+id/root"> 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.8" 
    tools:context="com.example.burhan.googlemapsdemoapp.MapsActivity" /> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.2" 
    android:gravity="bottom|center" 
    android:id="@+id/mapLayout"> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="whatever"/> 

</LinearLayout> 
</LinearLayout>