我有一個ScrollView,我想在其上添加2個圖像。
我創建了一個ScrollView,然後是一個LinearLayout。然後創建2個ImageView並將它們添加到LinearLayout中。然後在ScrollView上添加LinearLayout。 (所有的步驟由Java代碼。根本沒有使用XML)
但問題是ScrollView高度不適合我的圖像的高度(它太長了)。
這是我的代碼:ScrollView高度不適合其內容 - Android Java
LinearLayout scrollParent = new LinearLayout(this);
scrollParent.setOrientation(LinearLayout.VERTICAL);
scrollParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
helpView = new ScrollView(this);
helpView.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
helpView.setFillViewport(true);
inside = new LinearLayout(this);
inside.setOrientation(LinearLayout.VERTICAL);
inside.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
helpView.addView(inside);
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.learn1);
img.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ImageView img2 = new ImageView(this);
img2.setImageResource(R.drawable.learn2);
img2.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
inside.addView(img);
inside.addView(img2);
scrollParent.addView(helpView);
感謝您的幫助。
使用ScrollView.LayoutParams,而不是試圖在第2行 – HeisenBerg
RelativeLayout.LayoutParams,沒有工作@HeisenBerg – SAKT
嘗試LinearLayout.LayoutParams.MATCH_PARENT代替ViewGroup.LayoutParams.MATCH_PARENT – HeisenBerg