带换行符的多行 TextView
Multiline TextView with line breaks wrapping
我正在尝试为 Google 地图图钉创建自定义信息内容模板,但在包装多行 TextView
时,我 运行 遇到了一些奇怪的行为使用以下代码时,其中包含带有多个换行符的文本:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:orientation="vertical">
<ImageView
android:id="@+id/OfficeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:maxWidth="175dp"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/InfoWindowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sunshine Coast"
android:textColor="@android:color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/InfoWindowSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="10dp"
android:text="Level 3, 2 Emporio Place\n2 Maroochy Blvd, Maroochydore QLD 4558\nPo Box 5800, Maroochydore BC QLD 4558"
android:maxLines="10"
android:textColor="@android:color/black"/>
</LinearLayout>
结果:
如图所示,第一行换行后的文字缺失。如果没有换行,或者最后一行是换行,那么所有的线都会完美显示(见下图)。有谁知道如何让它正常工作?
Muhammad Babar's 建议解决了这个问题。我添加了一个 RelativeLayout 作为 LinearLayout 的父级,现在所有文本都已正确显示(请参阅下面的工作代码)。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:orientation="vertical">
<ImageView
android:id="@+id/OfficeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:maxWidth="175dp"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/InfoWindowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sunshine Coast"
android:textColor="@android:color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/InfoWindowSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="Level 3, 2 Emporio Place\n2 Maroochy Blvd, Maroochydore QLD 4558\nPo Box 5800, Maroochydore BC QLD 4558"
android:maxLines="10"
android:textColor="@android:color/black"/>
</LinearLayout>
</RelativeLayout>
我正在尝试为 Google 地图图钉创建自定义信息内容模板,但在包装多行 TextView
时,我 运行 遇到了一些奇怪的行为使用以下代码时,其中包含带有多个换行符的文本:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:orientation="vertical">
<ImageView
android:id="@+id/OfficeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:maxWidth="175dp"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/InfoWindowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sunshine Coast"
android:textColor="@android:color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/InfoWindowSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="10dp"
android:text="Level 3, 2 Emporio Place\n2 Maroochy Blvd, Maroochydore QLD 4558\nPo Box 5800, Maroochydore BC QLD 4558"
android:maxLines="10"
android:textColor="@android:color/black"/>
</LinearLayout>
结果:
如图所示,第一行换行后的文字缺失。如果没有换行,或者最后一行是换行,那么所有的线都会完美显示(见下图)。有谁知道如何让它正常工作?
Muhammad Babar's 建议解决了这个问题。我添加了一个 RelativeLayout 作为 LinearLayout 的父级,现在所有文本都已正确显示(请参阅下面的工作代码)。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:orientation="vertical">
<ImageView
android:id="@+id/OfficeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:maxWidth="175dp"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/InfoWindowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sunshine Coast"
android:textColor="@android:color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/InfoWindowSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="Level 3, 2 Emporio Place\n2 Maroochy Blvd, Maroochydore QLD 4558\nPo Box 5800, Maroochydore BC QLD 4558"
android:maxLines="10"
android:textColor="@android:color/black"/>
</LinearLayout>
</RelativeLayout>