动态地在Map Fragment下面添加textView

时间:2016-10-18 17:26:56

标签: android xml google-maps google-maps-markers

我一直在尝试以编程方式在谷歌地图片段下面的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);
}

1 个答案:

答案 0 :(得分: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>