【问题标题】:Wraping TextView When Text Goes Longer Then One Line当文本超过一行时包装 TextView
【发布时间】:2012-12-07 15:30:38
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:gravity="center"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">


<ProgressBar android:id="@+android:id/progress_small"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="15sp"
   android:gravity="center"
   android:text="LOREM IPSUM BAB BABALOREM IPSUM BAB BABALOREM"
   />

</LinearLayout>

Lorem 的 L 和进度条之间的间隙如何消除?我希望文本仅居中对齐。

textview 宽度是换行内容。但是两边都有差距。 如何摆脱它?

我也不打算单行它。因为我不想错过它显示的任何单词。

我也不希望左对齐

【问题讨论】:

  • 我不认为你可以故意实现你的目标,我认为这取决于android如何包装单词等,添加一个短单词可能会将第一行推向左侧。至少我的 S2 上会发生这种情况

标签: android xml android-layout layout layout-gravity


【解决方案1】:

在您的 textView 中进行此更改 ::

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="15sp"
          android:gravity="center"
          android:singleLine="True"  
          android:text="LOREM IPSUM BAB BABALOREM IPSUM BAB BABALOREM"/>

【讨论】:

  • 我不希望它是单行的。它应该以中心重心换行到下一行
  • 这不是我要找的:(
【解决方案2】:
style="?android:attr/progressBarStyleSmall"
android:layout_width="0dp"
android:layout_weight="1"

将progressBar的权重设置为1,宽度设置为0dp,你可以得到想要的结果,但不能保证它在所有屏幕尺寸上都有效

编辑:

通过上述更改,我能够获得所需的输出。

【讨论】:

  • Mani,它看起来像你刚刚发布的内容。但是尝试减少字符串的长度,看看那个间隙会再次变得更加突出。
  • 文本长度是动态的
【解决方案3】:

试试

android:gravity="center|left"

【讨论】:

  • 所以你想让第一行左对齐/全部在左边,第二行应该居中/仍然在中间?
  • 不完全是。我希望文本居中,但进度条在 Lorem 的 L 附近触摸
  • 想要一张截图看看你的目标是什么
【解决方案4】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<ProgressBar android:id="@+id/ProgressDisplay"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/ProgressDisplay"
    android:gravity="left"
    android:text="LOREM IPSUM BAB BABALOREM IPSUM BAB BABALOREM"
    android:textSize="15sp" />

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 2014-04-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多