簡體   English   中英

TextView-將視圖移到末尾

[英]TextView - Move view to end

我有多行TextView 如何確定最后的空間以了解視圖是否適合其中?

例:

--------------------
| 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>|
--------------------

因此,我希望視圖位於右下角,它不應覆蓋文本,但如果有足夠的空間,則應覆蓋在文本的右邊

基本上,您需要查看最后一行的右邊(y坐標)到達何處,然后計算其他視圖是否可以容納其余空間。

我在這里用2個textviews做了一個示例,但是它可以應用於您想要的任何視圖。 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。
  • 如果您的視圖有邊距和填充,則也需要計算 fe,如果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:

底部

對

我認為這個問題需要更多的背景。 (只是好奇為什么要這么做)

如果TextView具有在XML中定義的設置的寬度/高度,則這樣的事情是可能的,並且會更容易。 如果您只是動態地獲取信息,則必須采取一些步驟來確保獲得正確的度量。

我正在考慮的一種方法是獲取整個TextView的寬度,並獲取與實際文本寬度之間的差(也許是getLineCount()/getLineBounds(int, Rect?)getTextScaleX() ?的組合)。 然后將其轉換為dp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM