在android应用中动态创建TextViews

时间:2018-12-17 20:17:47

标签: android google-maps textview

我正在尝试使用地图创建android应用,并且希望创建一个textView,当用户单击Marker时将显示它。唯一的条件是视图必须可滚动(某些文本可能会很长),并且用户可以通过滑动或其他方法轻松地将其丢弃。

我尝试了以下操作:

 @Override
 public boolean onMarkerClick(final Marker marker) {

  TextView tv = new TextView(this);
  tv.setText(marker.getTag().toString());
 }

我没想到会有那么简单的工作,但是没想到。进一步的研究表明,我可能需要在布局xml文件中放置一个TextView,但由于我的配置或多或少已设置,因此我不愿意这样做。

FWIW,这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
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="match_parent"
tools:context=".Excursions" />
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".05"
    android:background="#000000"
    android:text="Excursions"
    android:textColor="#ffffff"
    android:gravity="center"/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    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=".85"
    tools:context=".Excursions" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".10"
    android:background="#004D79"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/addressText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="7dp"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:text="Enter address">
    </EditText>

    <Button
        android:id="@+id/findButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:text="Find">
        </Button>
</LinearLayout>

编辑:只是为了澄清我要做什么。 当您单击一个标记时,将出现一个TextView,它覆盖整个屏幕,并显示文本数据。由于我不知道将显示多少文本,因此应该有垂直滚动条。某种程度上,应该有一种方法可以关闭新的textview,以便将应用程序的外观恢复为单击标记之前的外观。

2 个答案:

答案 0 :(得分:0)

您需要设置布局参数

tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                                 LinearLayout.LayoutParams.WRAP_CONTENT));

以及将新视图添加到父版式

parentLayout.addView(tv);

答案 1 :(得分:0)

我认为您应该将此TextView添加到具有属性的xml

android:visibility="gone"

并更改此方法:

@Override public boolean onMarkerClick(final Marker marker) { 
    TextView tv = findViewById(R.id.text);
    tv.setText(text);
    tv.setVisibility(View.VISIBLE); 
}
相关问题