【问题标题】:Create a paragraph using multiple textviews using xml layout使用 xml 布局使用多个文本视图创建段落
【发布时间】:2013-08-14 16:34:55
【问题描述】:

这是我想要创建段落的布局文件。即下一句应该从上一句结束的地方开始。但这不会发生,下一句将在下一行出现。其实我是通过 xml 方法,因为每个句子都与数据库相关联并且必须由用户选择,所以没有选择使用\n。

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <ScrollView
        android:fillViewport="true"
        android:id="@+id/q_area"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/question_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:padding="10dp"
                android:text="Q1 : Select the right sentence out of these ?" />

            <LinearLayout
                android:orientation="vertical"
                android:padding="10dp"
                android:layout_below="@id/question_text"
                android:layout_marginTop="25dp"
                android:id="@+id/comprehension"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/sentence1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Reading comprehension is defined as the level of understanding of a text/message." />

                <TextView
                    android:id="@+id/sentence2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="This understanding comes from the interaction between the words that are written and how they trigger knowledge outside ." />

                <TextView
                    android:id="@+id/sentence3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Proficient reading depends on the ability to recognize words quickly and effortlessly." />

                <TextView
                    android:id="@+id/sentence4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Many educators in the USA believe that students need to learn to analyze text." />

                <TextView
                    android:id="@+id/sentence5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="But other US educators consider this reading approach to be completely backward." />
            </LinearLayout>

            <TextView
                android:id="@+id/explanation_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/comprehension"
                android:layout_marginTop="25dp"
                android:padding="10dp"
                android:text="Explanation: Sentence you have selected is right?" />

            <View
                android:visibility="invisible"
                android:id="@+id/user_optionset4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/explanation_text"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="50dp"
                />
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        android:background="@android:color/black"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:gravity="center"
            android:padding="0dp"
            android:singleLine="true"
            android:text="P"
            android:textColor="@android:color/black" />

        <Button
            android:id="@+id/Take"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.4"
            android:gravity="center"
            android:padding="0dp"
            android:singleLine="true"
            android:text="Done"
            android:textColor="@android:color/white" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:gravity="center"
            android:padding="0dp"
            android:singleLine="true"
            android:text="N"
            android:textColor="@android:color/black" />
    </LinearLayout>

</RelativeLayout>

这是当前的输出:

预期输出,下一句应该从黄色箭头的位置开始,如图:

【问题讨论】:

  • 你可以试试
    标签吗??

标签: android android-layout


【解决方案1】:

实现这一点的最简单方法可能是一个 webview,您将一些 css 附加到每个句子是一个链接的地方,并且该链接调用一个 Java 方法来完成所需的工作。然后由于所有链接都不是块,它们将在最后一行结束的地方继续,没有换行符(除非你特别声明一个)。

关于如何实现js调用Java的一些资料可以参考here

【讨论】:

  • 我终于这样做了
  • @prateek 如果您接受此答案,请在下方删除您自己的答案!
【解决方案2】:

您可以在每个文本的末尾使用
。但是,您需要在资源下的 string.xml 中定义您的字符串/消息。下面是一个例子:

<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>

更多详情可以参考以下链接: LINK

【讨论】:

  • 我为什么要使用&lt;br&gt; 我不希望下一行出现文本应该自动中断而不是基于句子完成
  • @Sushil 那么我应该怎么做才能避免这种情况,我认为 Box 布局只能在这种情况下提供帮助。所以选择了LinearLayout。
  • 为什么不使用单个文本视图?您需要使用
  • 最好的方法是使用
    等正确对齐 Strings.xml 文件中的段落字符串。然后使用带有该字符串的单个 textview。
  • @Sushil 我同意你的看法。
【解决方案3】:

这就是我在上面的 commnet 中的意思。您应该在 strings.xml 中创建一个字符串。使用
等在此处进行适当的 alignmnet。然后使用 TextView 中的字符串:

本隐私政策规定了 xxx 如何使用和保护您在使用此应用时向 xxx 提供的任何信息。\n\n

xxx 致力于确保您的隐私受到保护。如果我们要求您在使用此应用程序时提供某些可以识别您身份的信息,那么您可以放心,它只会按照本隐私声明使用。\n\n

xxx 可能会不时通过更新此页面来更改此政策。您应该不时检查此页面,以确保您对任何更改感到满意。该政策自 2013 年 6 月 1 日起生效。\n\n

我们收集什么\n\n

我们可能会收集以下信息:\n • 姓名和职务\n • 联系信息,包括电子邮件地址\n • 人口统计信息,例如邮政编码、偏好和兴趣\n • 与客户调查和/或优惠相关的其他信息\n 我们如何处理收集到的信息:\n\n

我们需要这些信息来了解您的需求并为您提供更好的服务,特别是出于以下原因:\n • 内部记录保存。\n • 我们可能会使用这些信息来改进我们的产品和服务。\n • 我们可能会定期使用您提供的电子邮件地址发送有关新产品、特别优惠或我们认为您可能会感兴趣的其他信息的促销电子邮件。\n
• 有时,我们也可能出于市场研究目的使用您的信息与您联系。我们可能会通过电子邮件、电话、传真或邮件与您联系。\n • 我们可能会根据您的兴趣使用这些信息来定制应用程序。\n\n

安全\n\n

我们致力于确保您的信息安全。为了防止未经授权的访问或披露,我们制定了适当的物理、电子和管理程序来保护和保护我们在线收集的信息。\n\n

我们如何使用 cookie\n\n

cookie 是一个小文件,它要求允许将其放置在您的本地存储中。一旦您同意,该文件将被添加并且 cookie 有助于分析网络流量或让您知道您何时访问特定网站。 Cookie 允许网络应用程序以个人身份响应您。通过收集和记住有关您的偏好的信息,该 Web 应用程序可以根据您的需要、好恶来调整其操作。\n 我们使用流量日志 cookie 来识别正在使用的页面。这有助于我们分析有关网页流量的数据并改进我们的应用程序,以便根据客户需求对其进行定制。我们仅将此信息用于统计分析目的,然后将数据从系统中删除。\n 总体而言,cookies 帮助我们为您提供更好的应用程序,使我们能够监控您认为哪些页面有用,哪些页面无用。除了您选择与我们共享的数据外,cookie 绝不允许我们访问您的计算机或有关您的任何信息。\n 您可以选择接受或拒绝Cookie。大多数浏览器会自动接受 cookie,但如果您愿意,通常可以修改浏览器设置以拒绝 cookie。这可能会阻止您充分利用该应用程序。

</string>

【讨论】:

  • 您可以将正确答案发布给其他人并接受它,以便其他人知道正确答案:)
【解决方案4】:

你可以在 android 中使用类似 flowlayout 的东西。 我从来没有机会使用它,但 FlowLayout 的自定义实现位于 https://github.com/ApmeM/android-flowlayout

注意:您需要弄清楚使用上述库的确切步骤和方法。也许有人可以帮助您解决这个问题

【讨论】:

    【解决方案5】:

    最后真正帮助我的是我不想去的webview。此外,我必须使用大量的 javascript 来完成我的工作,因为布局非常动态,webview 中显示的内容来自 sqlite。

    【讨论】:

    • @dreamsonfire 那么您在这里发表评论的意义何在?
    • 如你所愿,我删除了 :)
    猜你喜欢
    • 2012-04-26
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 2016-08-17
    相关资源
    最近更新 更多