2012-05-09 27 views
0

現在、私はJavaのコードでカスタムUIを作成したいし、xmlファイルを心配したくありません。私は、私のlinearLayoutの既存のtextViewの下にtextViewを追加したいところにいます。ここに私がこれまで持っているものがあります。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); 

問題は、mineralProperties textViewではなくmineralName textViewが追加されるということです。私はそれが非常に一番上にmineralName textView、それからmineralProperties textViewのすぐ下にしたいと思います。 LinearLayoutで

答えて

2

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

また、あなたがあなたのテキストビューレイアウトのparamsを変更する必要があります。他のをカバーするかもしれないそうでなければ

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

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

ビューのいずれかを。

1

あなたのコードは小さな変更で動作しています、それがあなたを助けることを願っています。 WRAP_CONTENTと

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

 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); 
関連する問題