0

現在、Java コードでカスタム UI を作成したいと考えており、xml ファイルについて心配する必要はありません。linearLayout の既存の textView の下に textView を追加したいところです。これが私がこれまでに持っているものです。

View linearLayout = findViewById(R.id.rockLayout);
        ImageView mineralPicture = new ImageView(this);
        TextView mineralName = new TextView(this);
        TextView mineralProperties = new TextView(this);
        mineralProperties.setText("The properties are: " + Statics.rockTable.get(rockName).getDistinctProp());
        mineralProperties.setId(2);
        mineralName.setText("This mineral is: " + rockName);
        mineralName.setId(1);
        mineralName.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.MATCH_PARENT));
        mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.MATCH_PARENT));
        /** Need to figure out the picture....
         * mineralPicture.setId(2);
         * mineralPicture.setImageResource(R.drawable.rocks);
         * mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
         */

            ((LinearLayout)linearLayout).addView(mineralName);
        ((LinearLayout)linearLayout).addView(mineralProperties);

問題は、mineralName textView のみを追加し、mineralProperties textView を追加しないことです。私はそれが一番上にあると思うので、ミネラル名のテキストビューを一番上に置き、次にミネラルプロパティのテキストビューをそのすぐ下に置きます。

4

2 に答える 2

2

LinearLayout の子ビューは、デフォルトで水平方向に積み重ねられます。で変更してみてくださいlinearLayout.setOrientation(LinearLayout.VERTICAL)

また、テキスト ビューのレイアウト パラメータを次のように変更する必要があります。

mineralName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

そうしないと、ビューの 1 つが他のビューを覆ってしまう可能性があります。

于 2012-05-09T08:31:01.060 に答える
1

あなたのコードは小さな変更で動作します。お役に立てば幸いです。

View linearLayout = findViewById(R.id.rockLayout);
       ImageView mineralPicture = new ImageView(this);
        TextView mineralName = new TextView(this);
        TextView mineralProperties = new TextView(this);
        mineralProperties.setText("The properties are: " + Statics.rockTable.get(rockName).getDistinctProp());

        mineralProperties.setId(2);
        mineralName.setText("This mineral is: " + rockName);
        mineralName.setId(1);

MATCH_PARENT を WRAP_CONTENT に変更

        mineralName.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        /** Need to figure out the picture....
         * mineralPicture.setId(2);
         * mineralPicture.setImageResource(R.drawable.rocks);
         * mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
         */

        ((LinearLayout)linearLayout).addView(mineralName);
        ((LinearLayout)linearLayout).addView(mineralProperties); 
于 2012-05-09T08:39:03.810 に答える