我寫了一個擴展的ViewGroup一類的ViewGroup和覆蓋的方法如下:如何添加子視圖使用RelativeLayout.LayoutParams
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for(int index = 0; index < getChildCount(); index++){
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
View child = getChildAt(index);
child.measure(View.MeasureSpec.makeMeasureSpec(width,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED));
}
}
當我添加子視圖的ViewGroup這個如下:
String word = words[random.nextInt(words.length)];
RelativeLayout layout = new RelativeLayout(DraggableGridViewSampleActivity.this);
TextView text = new TextView(DraggableGridViewSampleActivity.this);
text.setText(word);
ImageView view = new ImageView(DraggableGridViewSampleActivity.this);
view.setImageBitmap(getThumb(word));
layout.addView(view);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(text, params);
dgv.addView(layout);
效果如下所示:textView無法完全顯示。似乎父視圖沒有提供足夠的空間來顯示文本視圖。
如何有TextView的完全顯示?
分享它是如何現在看一些快照!在xml中執行此操作非常容易btw – khurram
對不起,我沒有權限 – MuziLee