但是在手机的世界里,要如何实现手机页面的转换呢? 最简单的方法就是改变Activity 的Layout !
在这个例子中,将布局两个Layout ,分别为Layout1(main.xml) 和Layout2(mylayout.xml), 默认的Layout 为main.xml, 我们在Layout1 当中创建一个按钮,当单击按钮时,显示第二个Layout(mylayout.xml) ;同样地,在Layout2 里也设计一个按钮,当单击第二个Layout 的按钮之后,刚显示回原来的Layout1 ,现在就来示范如何在两个页面之间互相切换.
下面是我们本程序所涉及的相关代码,首先是主界面布局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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎来到魏祝林的博客"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击进入Layout2"
/>
</LinearLayout>
其次我们在main.xml 同一目录新建一个为mylayout.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"
android:background="#ffffffff"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击进入Layout1"
/>
</LinearLayout>
最后是我们的核心程序setContentViewDemo.java
package com.android.setContentViewDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class setContentViewDemo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 载入main.xml Layout
setContentView(R.layout.main);
// 以findViewById()取得Button对象并添加事件onClickLisener
Button bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
goToLayout2();
}
});
}
// 将layout由main.xml切换成mylayout.xml
public void goToLayout2() {
// 将layout改成mylayout
setContentView(R.layout.mylayout);
Button b2 = (Button) findViewById(R.id.bt2);
b2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
goToLayout1();
}
});
}
// 将layout由mylayout.xml切换成main.xml
public void goToLayout1() {
setContentView(R.layout.main);
Button bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
goToLayout2();
}
});
}
}
最后执行之!,这一节就到此结束~
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id) Since: API Level 1 Callback method to be invoked when an item in this AdapterView has been clicked. Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item. Parameters : parent The AdapterView where the click happened. view The view within the AdapterView that was clicked (this will be a view provided by the adapter) position The position of the view in the adapter. id The row id of the item that was clicked.
public TabMenu(Context context,OnItemClickListener titleClick,OnItemClickListener bodyClick, MenuTitleAdapter titleAdapter,int colorBgTabMenu,int aniTabMenu){ super(context); mLayout = new LinearLayout(context); mLayout.setOrientation(LinearLayout.VERTICAL); //标题选项栏 gvTitle = new GridView(context); gvTitle.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); gvTitle.setNumColumns(titleAdapter.getCount()); gvTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); gvTitle.setVerticalSpacing(1); //纵向间距 gvTitle.setHorizontalSpacing(1); //横向间距 gvTitle.setGravity(Gravity.CENTER); gvTitle.setOnItemClickListener(titleClick); gvTitle.setAdapter(titleAdapter); gvTitle.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色 this.titleAdapter=titleAdapter; //子选项栏 gvBody = new GridView(context); gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色 gvBody.setNumColumns(4); gvBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); gvBody.setVerticalSpacing(10); gvBody.setHorizontalSpacing(10); gvBody.setPadding(10, 10, 10, 10); gvBody.setGravity(Gravity.CENTER); gvBody.setOnItemClickListener(bodyClick); mLayout.addView(gvTitle); mLayout.addView(gvBody); //设置默认项 this.setContentView(mLayout); this.setWidth(LayoutParams.FILL_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景 this.setAnimationStyle(aniTabMenu); this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应 }
private LinearLayout makeMenyBody(int position) { LinearLayout result=new LinearLayout(this.mContext); result.setOrientation(LinearLayout.VERTICAL); result.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL); result.setPadding(10, 10, 10, 10); TextView text = new TextView(this.mContext); text.setText(texts[position]); text.setTextSize(fontSize); text.setTextColor(fontColor); text.setGravity(Gravity.CENTER); text.setPadding(5, 5, 5, 5); ImageView img=new ImageView(this.mContext); img.setBackgroundResource(resID[position]); result.addView(img,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT))); result.addView(text); return result; }
//再熟悉不过了,这可以作为一个Gallery的Adapter public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); i.setImageResource(mThumbIds[position]); i.setAdjustViewBounds(true); i.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); i.setBackgroundResource(R.drawable.picture_frame); return i; } } private Integer[] mThumbIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
//makeView是ImageSwitcher回调的一个方法,当你设置了//mSwitcher.setFactory(this)之后,将掉用两次makeView public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return i; }
<TextView android:id="@+id/label" //什么意思? android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:padding="4dip" android:singleLine="true" android:color="?android:attr/textColorPrimaryInverse" //还有这个 android:background="#888" />
// Tell the media scanner about the new file so that it is // immediately available to the user.手动扫描特定文件 MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); } });
java基础试题和赛船游戏。