我有XML:機器人:只有一行是可見
<RelativeLayout
android:id="@+id/IcdSearchMainPanel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent" >
<own_components.SearchOutput
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="vertical" >
<!-- HERE ARE ADDED ROWS WITH RESULTS IN JAVA CODE (look into SearchOutput.java) -->
</own_components.SearchOutput>
</ScrollView>
</RelativeLayout>
我有類SearchOutput.java(其延伸的LinearLayout)用這樣的方法(通常它增加了一些圖形組件到行,然後將該行到滾動型):
public void setResultOutput(ResultContainer result)
{
for (int i = 0; i < result.size(); i++)
{
LinearLayout resultRow = new LinearLayout(getContext());
resultRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
resultRow.setOrientation(LinearLayout.HORIZONTAL);
resultRow.setGravity(Gravity.CENTER_VERTICAL);
if (i % result.getIterationStep() == 0)
{
resultRow.setPadding(0, 0, 0, 10);
}
addView(resultRow);
ImageView langFlag = new ImageView(resultRow.getContext());
langFlag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
try
{
Bitmap bitmap = BitmapFactory.decodeStream(MainClass.getAssetManager().open(Pathes.FLAGS_DIR + result.get(i)[0] + ".gif"));
langFlag.setImageBitmap(bitmap);
langFlag.setPadding(10, 0, 0, 0);
}
catch (IOException e)
{
Log.e("IMAGE_OPEN_EXC", e.toString());
}
resultRow.addView(langFlag);
TextView number = new TextView(resultRow.getContext());
number.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
number.setPadding(10, 0, 0, 0);
number.setText(result.get(i)[1]);
resultRow.addView(number);
TextView text = new TextView(resultRow.getContext());
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setPadding(20, 0, 0, 0);
text.setText(result.get(i)[2]);
resultRow.addView(text);
}
}
問題:
不管有多少元素在result
變量總是有一排showned:
問題:
我做錯了什麼?在我看來,邏輯上一切都是正確的。
另一個問題:這種方式有效嗎?也許我應該以不同的方式添加結果?我問,因爲可以有很多。
更換'resultRow.setOrientation(LinearLayout.HORIZONTAL);'和'resultRow.setOrientation(LinearLayout.VERTICAL);' –
我強烈建議使用ListView。 –
@達尼爾:它會導致只有標誌將在頂部,「A00.0」標誌下,文本在「A00.0」下。它不會讓其他行出現。 – rainbow