【发布时间】:2016-10-18 17:26:55
【问题描述】:
我一直在尝试以编程方式在谷歌地图片段下方的线性布局内添加一个文本视图,但似乎没有添加文本视图。这是我的 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);
}
【问题讨论】:
-
在添加 TextView 后尝试在根 LinearLayout 上调用 requestlayout()
-
为什么是 try/catch 块?
-
试图找出是否有任何异常...完全出于沮丧! @user1779222
标签: android xml google-maps google-maps-markers