目前在自学Android,根据一个网站提供的案例在学习 现在遇到一个问题 总是解决不了, 也搞不清什么地方搞错了,自定义的GridView就是显示不出来
这个案例是一个听歌猜歌曲名的游戏,GridView里面装的是24个Button按钮 每个按钮上面一个字 歌曲名称就在这些字中,通过选择正确的字来猜歌曲,问题是我现在自定义的GridView调试时完全显示不出来 代码如下
这是自定义的GridView类
这是启动的主类
模型类
自定义GridView中Button的XML
GridView所在的XML
mainactivity的XML
最后出来的效果是这样的 下面的GridView没有显示出来
这个案例是一个听歌猜歌曲名的游戏,GridView里面装的是24个Button按钮 每个按钮上面一个字 歌曲名称就在这些字中,通过选择正确的字来猜歌曲,问题是我现在自定义的GridView调试时完全显示不出来 代码如下
这是自定义的GridView类
package com.guessgame.redesignedUI;
import java.util.ArrayList;
import com.guessgame.model.WordButton;
import com.guessgame.ui.R;
import com.guessgame.utils.InflateUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
public class MyGridView extends GridView {
private ArrayList<WordButton> wdlist = new ArrayList<WordButton>();
private MyAdapter mAdapter;
private Context mContext;
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.mContext=context;
mAdapter = new MyAdapter();
this.setAdapter(mAdapter);
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return wdlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return wdlist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
WordButton wb;
if(convertView == null ){
convertView = InflateUtils.getView(mContext, R.layout.grid_button);
wb = wdlist.get(position);
wb.setmIndex(position);
wb.setMbutton((Button)findViewById(R.id.gridButton));
convertView.setTag(wb);
} else
wb = (WordButton)convertView.getTag();
return convertView;
}
}
public void updateDate(ArrayList<WordButton> arrayListButton){
this.wdlist = arrayListButton;
setAdapter(mAdapter);
}
}
这是启动的主类
package com.guessgame.ui;
import java.util.ArrayList;
import com.guessgame.model.WordButton;
import com.guessgame.redesignedUI.MyGridView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
public class GuessGameMain extends Activity{
private ImageButton play_button;
private LinearInterpolator mDiskLine;
private Animation mDiskAni;
private LinearInterpolator mDiskBarLineFadeIn;
private Animation mDiskBarFadeInAni;
private LinearInterpolator mDiskBarLineFadeOut;
private Animation mDiskBarFadeOutAni;
private Boolean isRunning = false;
private ImageView diskPic;
private ImageView diskBar;
private ArrayList<WordButton> wordButtons;
private MyGridView myGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
diskPic = (ImageView) findViewById(R.id.discPic);
diskBar = (ImageView) findViewById(R.id.discBarPic);
myGridView = (MyGridView) findViewById(R.id.gridbuttonView);
initCurrentStage();
play_button = (ImageButton) findViewById(R.id.discStartButton);
mDiskAni = AnimationUtils.loadAnimation(getApplicationContext(), R.animator.disc_rotate);
mDiskAni.setInterpolator(mDiskLine);
mDiskAni.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
isRunning = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
mDiskBarFadeInAni = AnimationUtils.loadAnimation(getApplicationContext(), R.animator.disk_bar_ani_fade_in);
mDiskBarFadeInAni.setFillAfter(true);
mDiskBarFadeInAni.setInterpolator(mDiskBarLineFadeIn);
mDiskBarFadeInAni.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
diskPic.startAnimation(mDiskAni);
}
});
mDiskBarFadeOutAni = AnimationUtils.loadAnimation(getApplicationContext(), R.animator.disk_bar_ani_fade_out);
mDiskBarFadeOutAni.setInterpolator(mDiskBarLineFadeOut);
mDiskBarFadeOutAni.setFillAfter(true);
mDiskBarFadeOutAni.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
play_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isRunning == false){
diskBar.startAnimation(mDiskBarFadeInAni);
isRunning = true;
}
}
});
}
@Override
public void onPause() {
diskPic.clearAnimation();
super.onPause();
}
public void initCurrentStage(){
wordButtons = initAllWord();
myGridView.updateDate(wordButtons);
}
public ArrayList<WordButton> initAllWord(){
ArrayList<WordButton> wbs = new ArrayList<WordButton>();
for(int i = 0; i<24;i++){
WordButton wb = new WordButton();
wb.setMbuttonString("好");
wbs.add(wb);
}
return wbs;
}
}
模型类
package com.guessgame.model;
import android.widget.Button;
public class WordButton {
private int mIndex;
private String mbuttonString;
private Button mbutton;
private Boolean isVisible;
public Boolean getIsVisible() {
return isVisible;
}
public void setIsVisible(Boolean isVisible) {
this.isVisible = isVisible;
}
public int getmIndex() {
return mIndex;
}
public void setmIndex(int mIndex) {
this.mIndex = mIndex;
}
public String getMbuttonString() {
return mbuttonString;
}
public void setMbuttonString(String mbuttonString) {
this.mbuttonString = mbuttonString;
}
public Button getMbutton() {
return mbutton;
}
public void setMbutton(Button mbutton) {
this.mbutton = mbutton;
}
public WordButton(){
this.isVisible = true;
this.mbuttonString="";
}
}
自定义GridView中Button的XML
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="@color/black"
android:background="@drawable/gridbuttonxml"
>
</Button>
GridView所在的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="match_parent"
android:orientation="vertical" >
<LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">"
</LinearLayout>
<ImageView android:id="@+id/buttonAreaLine" android:background="@drawable/game_line"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
<com.guessgame.redesignedUI.MyGridView
android:numColumns="8" android:layout_width="match_parent"
android:layout_weight="1" android:gravity="center"
android:id="@+id/gridbuttonView" android:verticalSpacing="2dp"
android:layout_height="match_parent"
>
</com.guessgame.redesignedUI.MyGridView>
</LinearLayout>
mainactivity的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/topbanner"/>
<include layout="@layout/discarea"/>
<include layout="@layout/wordarea"/>
</LinearLayout>
最后出来的效果是这样的 下面的GridView没有显示出来