2011-07-25 7 views
0

私はAndroidプロジェクトで作業しており、実行時にレイアウトにボタンを追加する必要があります。だから、これはレイアウトxmlです:レイアウトにランタイム時にボタンを追加する

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/layout"> 

</LinearLayout> 

そして、これは活動です:問題は、私が活動を開始すると、ボタン1ボタン2を「カバー」ということで、今

import android.app.Activity; 
import android.widget.*; 
import android.widget.TableLayout.LayoutParams; 
import android.os.Bundle; 

public class PluginsActivity extends Activity{ 

    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.plugins); 

     LinearLayout layout = (LinearLayout) findViewById(R.id.layout); 
     Button butt = new Button(this); 
     Button butt2 = new Button(this); 
     butt.setText("lol"); 
     butt2.setText("lol2"); 
     butt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     butt2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     layout.addView(butt); 
     layout.addView(butt2); 

    } 

} 

。実際、LayoutParams.WRAP_CONTENT、LayoutParams.WRAP_CONTENTを使用すると、両方のボタンが表示されます。 LinearLayoutに「新しいライン」を追加する方法はありますか?画面は次のようにする必要があります:


| * ** * ***Button1の* ** * ** * *** |


| * ** * ***Button2の* ** * ** * *** |


ありがとうございます。

+0

またあなた 'インポートandroid.widget.TableLayout.LayoutParamsを変更してください;'輸入android.widget 'へ.LinearLayout.LayoutParams; '。 – inazaruk

答えて

3

アンドロイドレイアウトファイルに

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

butt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
butt2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
+0

あなたは歓迎です – Rasel

+0

実行時にウィジェットを追加することをお勧めしますか? – kebyang

1

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

</LinearLayout> 

//言及をあなたのLinearLayoutのoritentation垂直を確認します:オリエンテーション= "垂直" 012 ....これは、主な問題は....それを垂直にすると動作しますし

0
Activity_Main.java 
____________________________________________________________________________ 

package com.example.dynamic; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     LinearLayout layout = (LinearLayout)findViewById(R.id.layout); 

     Button b1 = new Button(this); 
     b1.setText("A"); 

     Button b2 = new Button(this); 
     b2.setText("B"); 

     Button b3 = new Button(this); 
     b3.setText("C"); 

     Button b4 = new Button(this); 
     b4.setText("D"); 

     Button b5 = new Button(this); 
     b5.setText("E"); 

     Button b6 = new Button(this); 
     b6.setText("F"); 

     layout.addView(b1); 
     layout.addView(b2); 
     layout.addView(b3); 
     layout.addView(b4); 
     layout.addView(b5); 
     layout.addView(b6); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 
関連する問題