Layout程式清單
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/buttonExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />
</LinearLayout>
value程式清單
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test</string>
<string name="hello_world">Hello world!</string>
<string name="button">退出</string>
</resources>
MainActivity程式清單
package com.demo.test;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
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);
// 綁定Button
Button _buttonExit = (Button) findViewById(R.id.buttonExit);
//動态更改button文本
_buttonExit.setText("text change");
//綁定容器控件 LinearLayout ll
LinearLayout llLayout=(LinearLayout)findViewById(R.id.ll);
//動态生成一個Button
Button _newButton=new Button(this);
_newButton.setText("new button");
//将元件綁定到父容器上
llLayout.addView(_newButton, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
}