0

DB からいくつかのデータを取得しており、それを箇条書きリストの形式で表示する必要があります。

コードで:

for (int i=0; i<ingredcount; i++){
            String ingredient = recipe.Ingredients.get(i);
            View linearlayout = findViewById(R.id.llIngredients);
            ImageView img = new ImageView(RecipeIngredientsDisplay.this);
            TextView txt1 = new TextView(RecipeIngredientsDisplay.this);
            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            llp.setMargins(30, 2, 15, 2);
            LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            llp2.setMargins(10, 2, 15, 2);

            img.setLayoutParams(llp2);
            img.setImageResource(R.drawable.dot_purple);
            txt1.setLayoutParams(llp);

            txt1.setText(ingredient);
            ((LinearLayout)linearlayout).addView(img);
            ((LinearLayout) linearlayout).addView(txt1);

        }

すべてのデータが正しく表示されています。画像が新しい行にあり、テキストがその下にあるだけです。

したがって、プログラムで画像とテキストに重みを設定して、両方が 1 行に収まるようにしたいと思います。

通常、アダプターとインフレータブル listView を使用してこれを行いますが、ScrollView 内で scrollView と ListView を組み合わせるとうまく動作せず、多くの成分があるとフォーカスが失われます。

4

1 に答える 1

1

水平に設定された別の LinearLayout で ImageView と TextView を囲むことができます。

LinearLayout l = new LinearLayout( this );
l.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT );
l.addView( img );
l.addView( txt1 );
((LinearLayout)linearlayout).addView( l );

私はおそらくそのコードを少し台無しにしましたが、あなたはそのアイデアを理解していますか? LinearLayout はデフォルトで水平であるため、作成する新しいもので変更する必要はありません。

于 2012-10-18T20:24:58.213 に答える