0
嗨在我的應用程序中,我通過使用GridView和框架佈局來設計它,因爲我使用text.Text顯示圖像。我在項目中添加的項目現在我的問題是如何編寫單擊按鈕爲不同的項目。網格視圖android中的框架佈局
例如:如果我點擊關於我們,我想打開一些活動。如果我點擊照片庫,我想打開另一個活動。 任何人都可以請告訴我如何寫這一個。
MainActivity類別
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("About Us", R.drawable.aboutus));
items.add(new Item("Photo Gallery", R.drawable.photo));
items.add(new Item("Veg Food", R.drawable.veg));
items.add(new Item("Non Veg Food", R.drawable.nonveg));
items.add(new Item("Location", R.drawable.contactus));
items.add(new Item("Contact Us", R.drawable.contactus));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
}
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" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<com.agilerise.hotel.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:paddingTop="10dp"
android:paddingBottom="10dp"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="bottom"
android:textColor="@android:color/black"
/>
</FrameLayout>
<GridView
android:id="@+id/buttom"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:numColumns="3"
android:layout_weight="1">
</GridView>
</LinearLayout>
如果我使用這樣它的移動到同一個活動
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//Grab the item position here and write further code
switch (pos) {
case 0:
Intent nextScreen = new Intent(getApplicationContext(), Aboutus.class);
startActivity(nextScreen);
break;
case 1:
Intent nextScreen1 = new Intent(getApplicationContext(), FlickrActivity.class);
startActivity(nextScreen1);
break;
}
}
});
xml文件它正在改變設備爲什麼你可以告訴我我想要一個在中間 – user3739863