[英]Android get programmatically created view's width
我以編程方式創建TextView,如下所示:
TextView mTextView = new TextView(getApplicationContext());
final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((MarginLayoutParams) params).setMargins(8, 8, 0, 0);
mTextView.setText(mText);
mTextView.setLayoutParams(params);
mTextView.setPadding(2, 2, 2, 2);
mTextView.setBackground(getResources().getDrawable(R.drawable.note_corps));
tagMap.addView(mTextView);
textViewsWidth = mTextView.getWidth();
但是mTextView.getWidth()總是返回0
如果我嘗試:
mTextView.getLayoutParams().width
它返回LayoutParams類中的LayoutParams對應值(-1或-2)
如何獲得視圖的寬度?
編輯我需要在這里這樣做:
@Override
public void onPostExecute(Hashtable<String, Integer> hash){
final ScrollView tagMapScroll = (ScrollView) findViewById(R.id.tagMapScroll);
final LinearLayout tagMap = (LinearLayout) findViewById(R.id.tagMap);
final ArrayList<Integer> arr = new ArrayList<Integer>(hash.values());
final Enumeration<String> e = hash.keys();
int index = 0;
int textViewsWidth = 0;
while(e.hasMoreElements()){
final TextView tV = new TextView(getApplicationContext());
tV.setTextColor(Color.parseColor("#666666"));
tV.setText(Html.fromHtml(randomColor() + e.nextElement()));
tV.setTextSize(arr.get(index));
final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((MarginLayoutParams) params).setMargins(8, 8, 0, 0);
tV.setLayoutParams(params);
tV.setPadding(2, 2, 2, 2);
tV.setBackground(getResources().getDrawable(R.drawable.note_corps));
tagMap.addView(tV);
textViewsWidth += tV.getWidth();
index++;
}
tagMapScroll.setVisibility(ScrollView.VISIBLE);
}
編輯解決方案我從@ androiduser的答案中使用了這個:
mTextView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int width = mTextView.getMeasuredWidth();
int height = mTextView.getMeasuredHeight();
問題解決了 !
我用過這個解決方案:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Ensure you call it only once
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Here you can get the size :)
}
});
得到這樣的方式:
tV.post(new Runnable() {
@Override
public void run() {
int width=tV.getMeasuredWidth();
}
});
這些值是FILL_PARENT和WRAP_CONTENT的常量值。 如果要查看視圖大小,可以嘗試這種方式:
tagMap.addView(mTextView);
tagMap.post(new Runnable() {
@Override
public void run() {
mTextView. getWidth();
}
});
你必須等到android繪制TextView
。 這樣你就可以在tagMap隊列中發布一個Runnable,它被執行,希望在繪制textview之后(所以它應該是weight和height)
在此方法中,您可以獲得視圖的寬度高度。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int width = mTextView.getWidth();
int height = mTextView.getHeight();
}
並將TextView mTextView
定義為全局
您必須使用ViewTreeObserver類,該類用於注冊可以在視圖樹中通知全局更改的偵聽器。 這樣的全局事件包括但不限於整個樹的布局,繪圖通道的開始,觸摸模式的改變。
在Activity的onCreate mehotd中把這個:
ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int viewWidth = mTextView.getMeasuredWidth();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.