java - TextView - 将 View 移至末尾

标签 java android textview

我有一个包含多行的TextView。如何确定末尾的空间以了解 View 是否适合其中?

示例:

--------------------
| I am a multiline |
| text that has    |
| space at the     |
| end.       <View>|
--------------------

--------------------
| I am a multiline |
| text that has    |
| not enough space |
| at theeee end.   |
|            <View>|
--------------------

所以我希望我的 View 位于右下角,它不应该覆盖文本,但如果有足够的空间,则应该位于文本的右侧

最佳答案

基本上,您需要查看最后一行的右侧(y 坐标)到达哪里,然后计算另一个 View 是否可以容纳剩余的空间。

我在这里用 2 个 TextView 做了一个示例,但它可以应用于您想要的任何 View 。 xml 非常直接,没有什么异常。

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="kasjdf as df asd  aasdfasdff as dfa sd  sdf as df asd fa sd fa sdf as dfanas,df askdjfasjdf lkajsldkf jlaksjdfajsdkfj alskdj fklasjdflkasjdlkfdflk ajskldf" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="attached text"
        android:textColor="@android:color/holo_blue_dark" />

</RelativeLayout>

这里的问题是,在布局中绘制 text1 和 text2 后,我正在计算 text1 的最后一行到达的位置,并与 text2 的宽度一起计算 - 看看它是否可以适合屏幕的宽度。

  • 只有在绘制text1和text2之后,我才能检查宽度和右侧位置,因此我在setText2Position中等待它们。
  • 这里的要点 - 根据上述计算向 text2 添加规则。
  • 如果 View 有边距和填充,则也需要计算,如果 text2 有填充,则检查应为:

lastLineRight + text2.getWidth() + text2.getPaddingRight() + text2.getPaddingLeft() > screenWidth

public class MainActivity extends AppCompatActivity {

    private boolean[] areBothViewsDrawn = {false, false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView text1 = (TextView) findViewById(R.id.text1);
        final TextView text2 = (TextView) findViewById(R.id.text2);

        text1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                text1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                areBothViewsDrawn[0] = true;
                setText2Position(text1, text2);
            }
        });

        text2.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                text2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                areBothViewsDrawn[1] = true;
                setText2Position(text1, text2);
            }
        });

    }

    private void setText2Position(TextView text1, TextView text2) {
        if (!areBothViewsDrawn[0] || !areBothViewsDrawn[1])
            return;

        int lineCount = text1.getLayout().getLineCount();
        float lastLineRight = text1.getLayout().getLineRight(lineCount - 1);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text2.getLayoutParams();
        if (lastLineRight + text2.getWidth() > screenWidth) {
            //The width of the last line of text1 is too long for the text2 to fit, so text2 must be in another line
            params.addRule(RelativeLayout.BELOW, text1.getId());
        } else {
            //The width of the last line of text1 and text2 smaller then the application's width, so text2 can be in the same line as text1
            params.addRule(RelativeLayout.ALIGN_BOTTOM, text1.getId());
        }
    }
}

以下是我的代码的一些 poc:

bottom

right

关于java - TextView - 将 View 移至末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37472271/

相关文章:

java - 当我使用 hsqldb 时,我遇到 GUI (JavaFx) 问题

java - 我写了一个通用方法吗?

java - 如何在没有多个包装类的情况下反序列化 XML?

java - eclipse 会阻止您使用特定的类吗?

android - 如何使 TextView 完全像这样

java - 为什么我的 android Activity 偶尔会崩溃并显示 "No adapter attached; skipping layout"?

android - getSocketAddress() 方法会导致延迟,从而导致 Android 中的通信滞后

android - 如何检测用户何时打开/关闭 GPS 状态?

css - CSS重现基于磷光体的终端屏幕的效果

Android:无法更改textview中部分文本的颜色