【问题标题】:Add textView below Map Fragment Dynamically在 Map Fragment 下添加 textView 动态
【发布时间】: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


【解决方案1】:

将您的布局更改为此,并删除您的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多