文本视图文本未正确包装

时间:2014-09-21 19:58:33

标签: android xml android-layout android-activity

尝试让我的文字包裹在左边顶部的下方,但它不会让我感觉到。我甚至使用过wrap_content

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:screenOrientation="portrait"
    android:gravity="center_horizontal">

    <TextView
        android:text="@string/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@android:style/TextAppearance.Large"
        android:textColor="@color/wc"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="@string/yellow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            style="@android:style/TextAppearance.Medium"
            android:textColor="@color/yellow" />
        <TextView
            android:text="@string/teal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            style="@android:style/TextAppearance.Medium"
            android:textColor="@color/teal" />
        <TextView
            android:text="@string/red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            style="@android:style/TextAppearance.Medium"
            android:textColor="@color/red" />
        <TextView
            android:text="@string/and"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            style="@android:style/TextAppearance.Medium"
            android:textColor="@color/white" />
        <TextView
            android:text="@string/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            style="@android:style/TextAppearance.Medium"
            android:textColor="@color/white" />
        <TextView
            android:text="@string/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            style="@android:style/TextAppearance.Medium"
            android:textColor="@color/white" />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

视图中使用了两组宽度和高度(API here)。第一对是测量宽度和测量高度,并确定视图在其父级中的大小。

但是为了防止文本换行,你可以使用:

android:inputType="text"

这仅适用于所有文本都在单个TextView中的情况。

如果您想要一个允许您选择单个条目的实现,可能更好的解决方案是ListView(here),以便最终用户更容易导航和选择以及更容易工作在发展方面。

或者,如果要格式化样式的文本,请考虑在单个TextView中使用格式化技术。这是我在快速搜索中找到的tutorial

答案 1 :(得分:1)

你不能使用几个文本视图,因为一行中的几个TextView一个接一个地对齐,如果文本很大,你就不能简单地让最后一个文本视图。使用一个TextView和代码在html的帮助下编写您的文本以获得多种颜色。像这样:

TextView txt = (TextView)findViewById(R.id.my_text_view_id);
txt.setText(Html.fromHtml("<font color="yellow">Way out,</font><font color="blue"> DLR,</font><font color="red"> Central</font>< color="white">  and Northern</font>"));
相关问题