【问题标题】:Problem with aligning textView to the end of LinearLayout将 textView 对齐到 LinearLayout 末尾的问题
【发布时间】:2021-08-05 18:24:28
【问题描述】:

我正在制作聊天应用程序,并希望在消息应用程序中设置消息样式。

这是我为它们设置样式的代码:

        textView.setTextColor(Color.parseColor("#FFFFFFFF"));

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                  params.setMargins(400,0,10,10);
                      params.gravity = Gravity.END;
                        textView.setLayoutParams(params);

                        linearLayout.addView(textView);

这是我的 XML 代码:

         <ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="bottom"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="12dp"
    android:background="@drawable/background"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toTopOf="@+id/editTextTextPersonName2"
    app:layout_constraintTop_toBottomOf="@+id/name">

    <LinearLayout
        android:id="@+id/chatMessages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</ScrollView>

我的 TextView 被添加到这个 ^ 线性布局中。

这就是我在应用程序中收到的内容:(不要翻译消息,它们仅用于测试目的,它们很愚蠢)

我不知道为什么会出现这些差距,请有人帮助我。

【问题讨论】:

    标签: java android android-xml


    【解决方案1】:

    这是 textView 的正常行为。您可以在 SIGNAL 应用程序上输入一个长字进行测试,它会将其移至下一行,在上一行,您将看到与您进入相同的空间。

    如果你想改变textview中文本的对齐方式,那么你可以根据你的要求定义textView的textAlignment属性。

    【讨论】:

    • 但是怎么删除这个空间,我一定有办法,因为它看起来很难看
    • 为 textView 尝试属性 android:breakStrategy。基本上,它将长单词分成两行,目前,它正在将整个文本移动到下一行,在上面的行中留下一个不需要的空间。
    【解决方案2】:

    发生这种情况是因为您的消息的最后一个单词的长度很长,如果 Android 发现他无法将一系列字符(单词)放入当前行,那么它会在下一行输入它,所以要解决这个问题,您有 2 种不同的解决方案(或更多):

    1-但它可能与另一边存在差距

     textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
    

    2- 它将文本在其容器中居中,这可能与两侧有间隙

     textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 2022-08-15
      • 2014-10-15
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 2012-07-16
      • 2013-12-27
      相关资源
      最近更新 更多