代碼正在工作,但問題在於,某些元素在文本視圖中重複出現,有些元素被忽略。例如「我們是誰」會顯示兩次。此外,我無法集中textview元素。我已經發布了它的截圖。我是android編程新手,所以幫助我,任何人。重複gridview元素.....!
這是主要活動
public class MainActivity extends Activity {
GridView grid;
public WebView webView;
public int pos;
String[] desc = { "Who We Are", "What We Do", "Entrepreneur", "Scholarship", "Admission",
"Internship", "Industrial Visit", "Project", "Buy or Sell Projects", "Free Training", "College Registration",
"Information", "Feedback", "Contact" };
int[] imageId = { R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
GridDesign adapter = new GridDesign(MainActivity.this, desc, imageId);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
pos = position;
Intent intent = new Intent(MainActivity.this, webView.class);
startActivity(intent);
}
});
}
}
這是自定義網格視圖類。
public class GridDesign extends BaseAdapter{
private Context mContext;
private final String[] desc;
private final int[] Imageid;
public GridDesign(Context c, String[] web, int[] Imageid) {
mContext = c;
this.Imageid = Imageid;
this.desc = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return desc.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_element, parent, false);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid
.findViewById(R.id.grid_image);
textView.setText(desc[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
這是主活動佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="2"
android:stretchMode="spacingWidthUniform" />
</LinearLayout>
這是單個網格單元佈局
<?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" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="@string/im" >
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="23"
android:gravity="center"
android:layout_marginTop="10sp"
android:textSize="12sp" >
</TextView>
</LinearLayout>
謝謝你,工作得很好。 –
對網格元素內的textview對齊的一點幫助對我來說會有奇蹟。 –
由於你的'LinearLayout'是'vertical',我假設你想要在圖像下面居中的'TextView'?如果是這樣,你只需要在'TextView'上設置'android:layout_width ='match_parent''' – HexAndBugs