我正在尝试通过夸大我的布局来构建一个动物测验。测验由 1 张图片和 4 个答案组成(每页)
它适用于某些 Android 设备(例如:Samsung GS2),而我在其他设备(Galaxy note、GS3 ...)上都出现了空白屏幕,这些设备都在 4.0 上运行。
我不明白为什么。你能给我一些帮助吗?
-------------- 布局部分 ---------
cat_quiz.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/home_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/label_quiz_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="12dip"
android:textStyle="normal" >
</TextView>
</LinearLayout>
<include layout="@layout/cat_quiz_item" />
</LinearLayout>
</ScrollView>
cat_quiz_item.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:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.mycatfamily.widget.MyGallery
android:id="@+id/photogallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_item_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp" >
<TextView
android:id="@+id/label_quiz_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15dip"
android:textStyle="bold" >
</TextView>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<TextView
android:id="@+id/label_quiz_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingTop="10dp"
android:textSize="15dip"
android:textStyle="bold" >
</TextView>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/label_quiz_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingTop="10dp"
android:textSize="15dip"
android:textStyle="bold" >
</TextView>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/label_quiz_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingTop="10dp"
android:textSize="15dip"
android:textStyle="bold" >
</TextView>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/label_photo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:visibility="gone" >
</TextView>
<TextView
android:id="@+id/animal_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >
</TextView>
</TableRow>
</TableLayout>
</LinearLayout>
---------------- 适配器部分 ---------------------------
public class QuizAdaptor extends BaseAdapter {
private Activity context;
public static int[] ids;
public static int[] data;
public static String[] name;
public static String[] btn_1_name;
public static String[] btn_2_name;
public static String[] btn_3_name;
public static String[] btn_4_name;
public static int img_width=420;
public static int img_height=300;
public int score = 0;
public int answers=0;
private int cpt_popup=0;
public QuizAdaptor(Activity context) {
this.context = context;
}
public class ViewHolder{
public TextView animal_id;
public TextView text;
public ImageView image;
public TextView label_quiz_button_1;
public TextView label_quiz_button_2;
public TextView label_quiz_button_3;
public TextView label_quiz_button_4;
public MyGallery gallery;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView==null){
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.cat_quiz_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.animal_id=(TextView)view.findViewById(R.id.animal_id);
viewHolder.text=(TextView)view.findViewById(R.id.label_photo_name);
viewHolder.image=(ImageView)view.findViewById(R.id.img_item_gallery);
viewHolder.label_quiz_button_1=(TextView)view.findViewById(R.id.label_quiz_button_1);
viewHolder.label_quiz_button_2=(TextView)view.findViewById(R.id.label_quiz_button_2);
viewHolder.label_quiz_button_3=(TextView)view.findViewById(R.id.label_quiz_button_3);
viewHolder.label_quiz_button_4=(TextView)view.findViewById(R.id.label_quiz_button_4);
viewHolder.gallery = (MyGallery)view.findViewById(R.id.photogallery);
view.setTag(viewHolder);
}
else {
view = convertView;
}
final ViewHolder holder = (ViewHolder) view.getTag();
holder.animal_id.setText(String.valueOf(ids[position]));
holder.text.setText(name[position]);
final int stub_id=data[position];
holder.image.setImageResource(stub_id);
holder.label_quiz_button_1.setText(btn_1_name[position]);
holder.label_quiz_button_2.setText(btn_2_name[position]);
holder.label_quiz_button_3.setText(btn_3_name[position]);
holder.label_quiz_button_4.setText(btn_4_name[position]);
}
------------- 活动部分--------
public class CatQuiz extends Activity{
public String TAG = getClass().getSimpleName();
public MyGallery galleryView;
private TextView label_photo_name, animal_id;
private TextView label_quiz_button_1,label_quiz_button_2,label_quiz_button_3,label_quiz_button_4;
ArrayList<String> cat_arraylist;
private int selectedImagePosition = 0;
public String getRandomName(ArrayList<String> cat_array, String animal_name){
int rd = (int) Math.round(Math.random()*(cat_array.size()-1));
if((cat_array.get(rd)).equals(animal_name)){
return getRandomName(cat_array,animal_name);
}
else{
String rslt = cat_array.get(rd);
return rslt;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cat_quiz);
QuizAdaptor cs_gallery = new QuizAdaptor(this);
animal_id = (TextView) findViewById(R.id.animal_id);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
animal_id.setVisibility(View.GONE);
label_photo_name = (TextView) findViewById(R.id.label_photo_name);
label_photo_name.setVisibility(View.GONE);
label_quiz_button_1 = (TextView) findViewById(R.id.label_quiz_button_1);
label_quiz_button_2 = (TextView) findViewById(R.id.label_quiz_button_2);
label_quiz_button_3 = (TextView) findViewById(R.id.label_quiz_button_3);
label_quiz_button_4 = (TextView) findViewById(R.id.label_quiz_button_4);
CatBdd catBdd = new CatBdd(getApplicationContext());
catBdd.open();
List<Cat> animals = catBdd.getAllCatRandom();
cat_arraylist = catBdd.getDistinctCatNameArray();
final int size = 30;
int i = 0;
cs_gallery.data = new int[size];
cs_gallery.name = new String[size];
cs_gallery.ids = new int[size];
cs_gallery.btn_1_name = new String[size];
cs_gallery.btn_2_name = new String[size];
cs_gallery.btn_3_name = new String[size];
cs_gallery.btn_4_name = new String[size];
String animal_img_name;
String animal_name_btn1;
String animal_name_btn2;
String animal_name_btn3;
String animal_name_btn4;
while(i<size){
String animal_name = animals.get(i).getName();
int id_item = animals.get(i).getId();
int rd = (int) Math.round(Math.random()*4);
if(rd==1){
animal_img_name = animals.get(i).getCat_images_a();
animal_name_btn1 = animal_name;
animal_name_btn2 = getRandomName(cat_arraylist, animal_name);
animal_name_btn3 = getRandomName(cat_arraylist, animal_name);
animal_name_btn4 =getRandomName(cat_arraylist, animal_name);
}
else if(rd==2){
animal_img_name = animals.get(i).getCat_images_b();
animal_name_btn1 = getRandomName(cat_arraylist, animal_name);
animal_name_btn2 = animal_name;
animal_name_btn3 = getRandomName(cat_arraylist, animal_name);
animal_name_btn4 =getRandomName(cat_arraylist, animal_name);
}
else if(rd==3){
animal_img_name = animals.get(i).getCat_images_c();
animal_name_btn1 = getRandomName(cat_arraylist, animal_name);
animal_name_btn2 = getRandomName(cat_arraylist, animal_name);
animal_name_btn3 = animal_name;
animal_name_btn4 =getRandomName(cat_arraylist, animal_name);
}
else {
animal_img_name = animals.get(i).getCat_images_d();
animal_name_btn1 = getRandomName(cat_arraylist, animal_name);
animal_name_btn2 = getRandomName(cat_arraylist, animal_name);
animal_name_btn3 = getRandomName(cat_arraylist, animal_name);
animal_name_btn4 =animal_name;
}
cs_gallery.ids[i] = id_item;
cs_gallery.name[i] = animal_name;
cs_gallery.data[i] = Integer.valueOf(animal_img_name);
cs_gallery.btn_1_name[i] = animal_name_btn1;
cs_gallery.btn_2_name[i] = animal_name_btn2;
cs_gallery.btn_3_name[i] = animal_name_btn3;
cs_gallery.btn_4_name[i] = animal_name_btn4;
i++;
}