2013-05-07 45 views
0

我想添加新的textViews到我的自定義線性佈局。但在繪製線性佈局之前,我想檢查它是否仍然適合,而不與右側的按鈕重疊,如果它重疊,我想在繪製它之前剪切textView中的文本。如何檢查動態視圖是否與另一個視圖重疊?

我的佈局

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="60dp" > 

<ImageButton 
    android:id="@+id/b_settings" 
    android:layout_width="50dp" 
    android:layout_height="44dp" 
    android:layout_alignParentRight="true" 
    android:layout_margin="0dp" 
    android:src="@drawable/watch_settings2x" /> 

<Button 
    android:id="@+id/b_test" 
    android:layout_width="50dp" 
    android:layout_height="44dp" 
    android:layout_toLeftOf="@id/b_settings" 
    /> 

<com.client.views.Breadcrump 
    android:id="@+id/ll_breadcrump" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:orientation="horizontal" /> 
</RelativeLayout> 

我的流程是這樣的,首先我添加了一個新的TextView到我的自定義視圖。

this.addView(TextView, position); 

我已實現了以下GlobalLayoutListener自定義視圖中繪製之前被稱爲,但寬度是媒體鏈接可供選擇:

@Override 
      public void onGlobalLayout() { 

       cutBreadcrump(); 
      } 

然後我先拿到的可用空間我Breadcrump(自定義視圖)

int fragmentWidth = getView().getWidth(); 

     int widthButtons = mIBSettings.getWidth() + mBTest.getWidth(); 

     int spaceBreadcrump = fragmentWidth - widthButtons; 

接下來,我從我的新添加的TextView切割個字符作爲和檢查新寬度

CharSequence text = textView.get(0).getText(); 
       text = text.subSequence(0, text.length() - 1); 
       textView.get(0).setText(text); 

但這是我的問題。改變文字後寬度不會改變。

mElements.get(0).getWidth(); 

我也試過paint.measureText(string);方法讓我的TextView的寬度,但它總是多到長。也許這種方法確實不同的東西,然後我的預期......

所以我的問題是在短如何改變其文本繪製視圖

+0

使用'view.getLocationInWindow(int [] coords)'應該讓你看看這些視圖是否重疊。 – Luksprog 2013-05-07 11:33:22

+0

在屏幕上繪製視圖之前,是否可以更好地檢查它? – AdrianoCelentano 2013-05-07 12:28:05

+0

你不能這樣做,如果視圖還沒有繪製在屏幕上沒有座標。當您添加視圖時,會在視圖中發佈一個'Runnable',您將在其中使用上述方法檢索座標。 – Luksprog 2013-05-07 12:35:12

回答

0

確定之後纔得到一個視圖的新寬度我終於做到了像這個我覆蓋了我的觀點的onLayout方法。所以當這個方法被調用時,它會保存我視圖的靜態屬性中的可用空間。 當我添加一個新的TextView我檢查與此幫助的所有字符串的總和: measure text return pixels based

和我剪我的琴絃有一點個人algorythm直到所有TextViews適合的空間。之後我畫了TextViews。

相關問題