在onCreate的方法中加载模板2种主要方式:
1:
setContentView(R.layout.activity_main);
2:
LinearLayout mainLinearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView( mainLinearLayout );
主页面布局 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".main">
<TextView
android:text="@string/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按钮1"/>
</LinearLayout>
副页面布局 test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="textView2"/>
</LinearLayout>
activety_main的入口文件 main.java : 包含findViewById方法, implements OnClickListener继承点击事件
package com.demo0.nono.demo_view;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Button;
public class main extends Activity implements OnClickListener {
private Button button1;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
LinearLayout mainLinearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView( mainLinearLayout );
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
LinearLayout testLinearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.test,null);
mainLinearLayout.addView(testLinearLayout);
editText = new EditText(this);
editText.setText("edit test content");
editText.setGravity(Gravity.LEFT);
editText.setSingleLine(false);
Log.d("init","app init");
mainLinearLayout.addView(editText, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
@Override
public void onClick(View view) {
Log.d("click","click");
int val = editText.getGravity() & 0x07;
if( val == Gravity.LEFT ){
editText.setGravity(Gravity.CENTER_HORIZONTAL);
}else if( val == Gravity.CENTER_HORIZONTAL ) {
editText.setGravity(Gravity.RIGHT);
}else{
editText.setGravity(Gravity.LEFT);
}
}
}
天道酬勤