我在Android中工作,我正在动态地将视图添加到线性布局中。我从ViewTreeObserver
动态添加视图的高度,但在ViewTreeObserver
内实际设置高度后,它不会改变。请查看我的代码
的onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wwww);
ScrollView parentScrollView = (ScrollView) findViewById(R.id.parentscroll);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.subscroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
((ScrollView) findViewById(R.id.subscroll))
.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
new TESTT().execute();
final LinearLayout tv = (LinearLayout) findViewById(R.id.subscrollLinearLayout);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
if (tv.getChildAt(0) != null) {
int btnsize = tv.getChildAt(0).getHeight();
ScrollView scrolllayout = (ScrollView) findViewById(R.id.subscroll);
scrolllayout.getLayoutParams().height = btnsize * 2;
}
}
});
}
private class TESTT extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(4000);//Time taken to consume the web service. If there
// is no delay the code work correct
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final LinearLayout layout = (LinearLayout) findViewById(R.id.subscrollLinearLayout);
List<String> mSempLog = new ArrayList<String>();
mSempLog.add("TEST1");
mSempLog.add("TEST2");
mSempLog.add("TEST3");
mSempLog.add("TEST4");
mSempLog.add("TEST5");
mSempLog.add("TEST6");
mSempLog.add("TEST7");
mSempLog.add("TEST8");
for (int i = 0; i < 10; i++) {
LinearLayout service_row = (LinearLayout) inflater.inflate(
R.layout.summary_list_row, null);
TextView mServiceSlNo = (TextView) service_row
.findViewById(R.id.summary_list_textslNo);
TextView mServiceText = (TextView) service_row
.findViewById(R.id.summary_list_textService);
TextView mStartDate = (TextView) service_row
.findViewById(R.id.summaryview_list_initiated_text);
TextView mEndDate = (TextView) service_row
.findViewById(R.id.summaryview_list_completed_text);
TextView mFullyEngaged = (TextView) service_row
.findViewById(R.id.summaryview_fully_count_text);
TextView mProgress = (TextView) service_row
.findViewById(R.id.summaryview_listentry_TextView);
LinearLayout mServiceLinearLayout = (LinearLayout) service_row
.findViewById(R.id.summary_list_sercicelayout);
mServiceSlNo.setText(Integer.toString(i + 1) + ".");
mServiceText.setText("fdgdfgdfg");
mStartDate.setText("fdgdfgdfg");
mFullyEngaged.setText("fdgdfgdfg");
mServiceLinearLayout.setTag(i);
layout.addView(service_row, i);
}
}
}
答案 0 :(得分:1)
您要添加的内容有一些布局,请将其放在layout.xml文件中
并使用此方法在父布局中添加新视图。
LinearLayout lay_toAddItems = (LinearLayout) rootView
.findViewById(R.id.lay_toAddItems);
View hiddenInfo = orderDetails.getLayoutInflater().inflate(
R.layout.include_items_invieworder, lay_toAddItems,
false);
lay_toAddItems.addView(hiddenInfo);
已编辑的部分:
LinearLayout layoutDialog = (LinearLayout) findViewById(R.id.lay_forgotpassword);
layoutDialog.setLayoutParams(new FrameLayout.LayoutParams(dialogWidth*3/4+dialogWidth*1/8,LayoutParams.WRAP_CONTENT));