我一直在创建一个应用程序,它的第一个活动是作为一个有6个项目的gridView,
我创建了一个自定义适配器,Grid中的每个项目都包含一个图像,并在其下面有一个textView
。问题是这个应用程序在模拟器中运行但是当我在手机上尝试它时我添加的图像图标被放错了(抱歉,我没有图像)。
以下是模拟器中的图像,
当我在手机上运行时 pig.png位居第一 mouse.png排在第2位, 其余的图标也放错了地方......它表现得很烦人,因为示例视频选项用于播放来自youtube的示例视频,播放视频后菜单项重置到正确的位置(如上面的jpeg)我不知道我做错了什么。 这是我的代码
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Learnixmba" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="MBA Online Video "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/CaptionText"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/tution_class"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/CaptionText"
android:textStyle="bold" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/www_learnixmba_com"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20sp" />
<GridView
android:id="@+id/gridMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="10dp"
android:fadingEdge="vertical|horizontal"
android:fastScrollEnabled="true"
android:numColumns="3" >
</GridView>
这是row.xml(对于Gridview)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/textview"
android:textColor="@color/CaptionText" />
和主java文件
package info.learnixmba;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Learnixmba extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learnixmba);
Common.setBackGround(this);
GridView mainGrid = (GridView)findViewById(R.id.gridMain);
mainGrid.setAdapter(new ImageAdapter(this));
// setting onItemClickListner
mainGrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent go = new Intent();
switch(position){
case 0:
go = new Intent(getBaseContext(),Universities.class);
break;
case 1:
go = new Intent(getBaseContext(),AboutUs.class);
break;
case 2:
go = new Intent(getBaseContext(),Faq.class);
break;
case 3:
go = new Intent(getBaseContext(),SampleVideos.class);
break;
case 4:
go = new Intent(getBaseContext(),HowToEnroll.class);
break;
case 5:
go = new Intent(getBaseContext(),Benefits.class);
break;
}startActivity(go);
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context MyContext;
public ImageAdapter(Context _MyContext)
{
MyContext = _MyContext;
}
@Override
public int getCount()
{
/* Set the number of element we want on the grid */
return 6;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View MyView = convertView;
if ( convertView == null )
{
/*we define the view that will display on the grid*/
//Inflate the layout
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.griditem, null);
// Add The Text!!!
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
// Add The Image!!!
ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
switch(position){
case 0:
tv.setText("Universities");
iv.setImageResource(R.drawable.uni);
break;
case 1:
tv.setText("About Us" );
iv.setImageResource(R.drawable.inf);
break;
case 2:
tv.setText("FAQ");
iv.setImageResource(R.drawable.faq);
break;
case 3:
tv.setText("Sample Videos" );
iv.setImageResource(R.drawable.sample);
break;
case 4:
tv.setText("How To Enroll?");
iv.setImageResource(R.drawable.enroll);
break;
case 5:
tv.setText("Benefits" );
iv.setImageResource(R.drawable.benefits);
break;
}
}
return MyView;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
}