【问题标题】:Android LayoutGravity with ScrollView, TableRow and TextView带有 ScrollView、TableRow 和 TextView 的 Android LayoutGravity
【发布时间】:2014-09-24 16:13:07
【问题描述】:

我有一个ScrollView 的设置,其中包含一个TableLayout 和一些TableRows。 其中一个TableRows 有一个TextView 作为一个孩子。

所有元素都居中,包括TextView,但我想将文本视图中的文本左对齐。

如果 textview 不删除我的 \n\t,我发现没有办法做到这一点。

android:textAlignment 也不起作用,我不想使用它,因为它需要最低 API 级别。

有没有一种方法可以在居中的文本视图中对齐文本?

一些代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_gravity="start">

<TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="430dp"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:layout_gravity="start"
         >
    <TableRow
        android:id="@+id/tableRow_question_headline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="5dip" >
        <TextView
            android:id="@+id/quiz_question_headline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="4"
            android:gravity="center"
         android:layout_margin="10dip"
     android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="5dip" >
        <!-- The textview, which text should be left-aligned -->
        <TextView
            android:id="@+id/quiz_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="4"
            android:layout_margin="10dip"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>

...

【问题讨论】:

  • 从 TextView 中移除这一行 android:gravity="center"。
  • @Haresh 错误的文本视图,我在重要的文本视图上方添加了评论。它没有 android:gravity 行。
  • 您的 TextView 文本包含 \t 或 \n ?
  • 是的,确实可以,稍后缩进一些显示在那里的代码。
  • 然后用空格简单替换。

标签: android textview text-alignment text-align


【解决方案1】:

在您的 TextView 中使用 android:gravity="left" 而不是 android:gravity="center"

【讨论】:

  • android:gravity="left" 对齐整个textview,但我不想要这个,我只想对齐这个textview中的文本。
  • 默认TextView文本重力是左的。
  • 不,整个文本视图不可能对齐。请更新您编辑的答案
  • @Haresh 我的解决方案没有错,你不应该投反对票
  • @jaimin,冷静下来,我只是试着解释一下,我从不拒绝你的问题,好吧....
猜你喜欢
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多