Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android 動態實現添加組件

Android 動態實現添加組件

編輯:高級開發

 main.XML文件內容如下:

  < ?XML version="1.0" encoding="utf-8"?>

  < LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"

  android:orIEntation="vertical" android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  < TextView android:id="@+id/textVIEw1" android:layout_width="fill_parent"

  android:layout_height="wrap_content" android:text="@string/hello" />

  < Button android:id="@+id/btnTV" android:layout_width="wrap_content"

  android:layout_height="wrap_content" android:text="@string/btn" />

  < /LinearLayout>

  test.XML文件內容如下:

  < ?XML version="1.0" encoding="utf-8"?>

  < LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"

  android:orIEntation="vertical" android:layout_width="wrap_content"

  android:layout_height="wrap_content">

  < TextVIEw android:layout_width="fill_parent"

  android:layout_height="wrap_content" android:text="@string/tvSecond" />

  < /LinearLayout>

  對應Main.Java內容如下:

  public class Main extends Activity implements OnClickListener {

  private Button btnTV;

  private TextView textVIEw1;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  // setContentVIEw(R.layout.main);

  LinearLayout mainLinearLayout = (LinearLayout) this.getLayoutInflater()

  .inflate(R.layout.main, null);

  setContentVIEw(mainLinearLayout);

  btnTV = (Button) this.findVIEwById(R.id.btnTV);

  textView1 = (TextView) this.findViewById(R.id.textVIEw1);

  btnTV.setOnClickListener(this);

  LinearLayout testLinearLayout = (LinearLayout) this.getLayoutInflater()

  .inflate(R.layout.test, mainLinearLayout);

  接上頁

  EditText editText = new EditText(this);

  editText.setSingleLine(false);

  editText.setGravity(Gravity.LEFT);

  mainLinearLayout.addVIEw(editText, new LinearLayout.LayoutParams(

  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

  }

  @Override

  public void onClick(VIEw v) {

  int value = textVIEw1.getGravity() & 0x07;

  if (value == Gravity.LEFT) {

  textVIEw1.setGravity(Gravity.CENTER_HORIZONTAL);

  } else {

  if (value == Gravity.CENTER_HORIZONTAL) {

  textVIEw1.setGravity(Gravity.RIGHT);

  } else {

  textVIEw1.setGravity(Gravity.LEFT);

  }

  }

  }

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved