Android アプリ開発では、プログラムの中でビューを作ることもできます。以下のサンプルでは、7つのボタンを配置して、その後さらに、OKとCancelのボタンを作成しています。
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
Button[] buttons ;
int num_arr;
num_arr=7;
buttons = new Button[num_arr];
int i =0;
while ( i < num_arr){
buttons[i] = new Button(this);
buttons[i].setText("number" + i );
buttons[i].setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(buttons[i]);
i=i+1;
}
Button button1 = new Button(this);
button1.setText("OK");
button1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(button1);
Button button2 = new Button(this);
button2.setText("Cancel");
button2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(button2);
}
}
0 件のコメント:
コメントを投稿