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);
}
}
}
}
<?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);
}
}
}
}