您好,登錄后才能下訂單哦!
代碼實現后界面如圖:
一。xml文件
1.<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/relativelayout">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="當前時間:"
android:textSize="16dp" />
<GridView
android:id="@+id/gv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/text1"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp" >
</GridView>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/text1"
android:text="----------"
android:textSize="16dp" />
</RelativeLayout>
(注text是當前時間顯示,不用管,直接一個GridView即可)
2.每個圖標的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/p_w_picpathview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:scaleType="fitXY"
android:layout_gravity="center"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"/>
</LinearLayout>
二。界面很簡單,下面是MainAcyivity代碼:
1.public class MainActivity extends Activity {
GridView gridview;
PictureAdapter adapter;
public static int colorcolor=Color.WHITE;
int[] p_w_picpaths=new int[]{R.drawable.newdate,R.drawable.beiwang,R.drawable.newout,R.drawable.newin,R.drawable.alldate,R.drawable.allbeiwang,R.drawable.allout,R.drawable.allin,R.drawable.personmanager,R.drawable.game,R.drawable.chat,R.drawable.relax,R.drawable.seeitlogo,R.drawable.aboutflag,R.drawable.settings,R.drawable.out};
String[] titles=new String[]{"寫日記","寫備忘","新增支出","新增收入","我的日記","我的備忘","總支出","總收入","個人管理","游戲娛樂","聊天室","最簡瀏覽器","待開發。。。","關于我","系統設置","退出"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview=(GridView)findViewById(R.id.gv1);
adapter=new PictureAdapter(titles,p_w_picpaths,this);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent=null;
switch(arg2){
case 0:intent=new Intent(MainActivity.this,newDate.class);
startActivity(intent);
finish();
break;
case 1:intent=new Intent(MainActivity.this,newMark.class);
startActivity(intent);
finish();
break;
case 2:intent=new Intent(MainActivity.this,newOut.class);
startActivity(intent);
finish();
break;
case 3:intent=new Intent(MainActivity.this,newIn.class);
startActivity(intent);
finish();
break;
case 4:intent=new Intent(MainActivity.this,allDate.class);
startActivity(intent);
finish();
break;
case 5:intent=new Intent(MainActivity.this,allMark.class);
startActivity(intent);
finish();break;
case 6:intent=new Intent(MainActivity.this,allOut.class);
startActivity(intent);
finish();
break;
case 7:intent=new Intent(MainActivity.this,allIn.class);
startActivity(intent);
finish();
break;
case 8:intent=new Intent(MainActivity.this,PersonManager.class);
startActivity(intent);
finish();
break;
case 9:intent=new Intent(MainActivity.this,Game.class);
startActivity(intent);
finish();
break;
case 10:intent=new Intent(MainActivity.this,Chat.class);
startActivity(intent);
finish();
break;
case 11:intent=new Intent(MainActivity.this,WebGo.class);
startActivity(intent);
finish();
break;
case 12:intent=new Intent(MainActivity.this,TabsFrame.class);
startActivity(intent);
finish();
break;
case 13:intent=new Intent(MainActivity.this,Aboutauthor.class);
startActivity(intent);
finish();
break;
case 14:MainActivity.this.openOptionsMenu();
break;
case 15:showdialog(MainActivity.this);
break;
}
}
});
}
2.PictureAdapter類來自BaseAdapter自己寫:
package com.example.licaizhuanjia;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class PictureAdapter extends BaseAdapter {
private List<Picture> pictures;
private LayoutInflater inflater;
public static ViewHolder vh;
public PictureAdapter(String[] titles,int[] p_w_picpaths,Context context){
super();
pictures=new ArrayList<Picture>();
inflater=LayoutInflater.from(context);
for(int i=0;i<p_w_picpaths.length;i++){
Picture picture=new Picture(titles[i],p_w_picpaths[i]);
pictures.add(picture);
}
}
public int getCount() {
// TODO Auto-generated method stub
if(null!=pictures){
return pictures.size();
}
else
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return pictures.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1==null){
arg1=inflater.inflate(R.layout.gv1p_w_picpath, null);
vh=new ViewHolder();
vh.title=(TextView)arg1.findViewById(R.id.textview);
vh.p_w_picpath=(ImageView)arg1.findViewById(R.id.p_w_picpathview);
arg1.setTag(vh);
}
else
vh=(ViewHolder)arg1.getTag();
vh.title.setText(pictures.get(arg0).getTitle());
settextcolor(MainActivity.colorcolor);
vh.p_w_picpath.setImageResource(pictures.get(arg0).getImage());
return arg1;
}
public static void settextcolor(int color){
vh.title.setTextColor(color);
}
}
3.Picture類
package com.example.licaizhuanjia;
public class Picture {
private String title;
private int p_w_picpath;
public Picture(){
super();
}
public Picture(String title,int p_w_picpath){
super();
this.title=title;
this.p_w_picpath=p_w_picpath;
}
public void setImage(int p_w_picpath){
this.p_w_picpath=p_w_picpath;
}
public void setTitle(String title){
this.title=title;
}
public int getImage(){
return p_w_picpath;
}
public String getTitle(){
return title;
}
}
4.ViewHolder類
package com.example.licaizhuanjia;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewHolder {
public TextView title;
public ImageView p_w_picpath;
}
(ps:可以吧picture類和ViewHolder類寫進PictureAdapter類,一樣的效果)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。