在android中的网格视图下添加带链接的按钮

时间:2015-01-09 14:11:00

标签: android

我编写了一个网格视图代码,它在网格视图中显示图像。在这里,我的问题是在网格视图下添加一个带链接的按钮。(如果用户点击按钮,那么它应该导航到相应的链接)

这是我的java代码:

     public class some_class extends ActionBarActivity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
           setContentView(R.layout.gridview);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
              }
         public class ImageAdapter extends BaseAdapter{
          private Context mContext;
            public int getCount() {
        return mThumbIds.length;
            }
              public Object getItem(int position) {
               return mThumbIds[position];
               }
                public long getItemId(int position) {
               return 0;
                }
              public ImageAdapter(Context c) {
          mContext = c;
           }

             public View getView(int position, View convertView, ViewGroup parent) {
              ImageView imageView;
            if (convertView == null){
                 imageView = new ImageView(mContext);
                  imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                  imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                   imageView.setPadding(8, 8, 8, 8);
                     }
                          else{
                    imageView = (ImageView) convertView;
                   }
                        imageView.setImageResource(mThumbIds[position]);
                  return imageView;
                      }

                      private Integer[] mThumbIds = {
                                   R.drawable.minee1,
                      R.drawable.minee2, R.drawable.minee3,
                     R.drawable.minee4, R.drawable.minee5,
                          R.drawable.minee6, R.drawable.minee7,
                     R.drawable.minee8, R.drawable.minee9,
                          R.drawable.minee10, R.drawable.minee11,


                };

              }



                    @Override
                   public boolean onCreateOptionsMenu(Menu menu) {

                    getMenuInflater().inflate(R.menu.bagavan_pics, menu);
               return true;
             }

                 @Override
              public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
                if (id == R.id.action_settings) {
             return true;
               }
             return super.onOptionsItemSelected(item);
               }
                 }

我的相应网格视图XML是:                                            >           

我的相应网格视图XML是:

             <?xml version="1.0" encoding="utf-8"?>
          <GridView xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/gridview"
          android:layout_width="fill_parent"
            android:layout_height="fill_parent"
         android:numColumns="auto_fit"
      android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
         android:verticalSpacing="10dp"
      android:gravity="center"
        android:stretchMode="columnWidth"  
      >
   </GridView>     

1 个答案:

答案 0 :(得分:0)

在您的java代码中:

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = "your url here";

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
            });

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clipChildren="true"
        android:columnWidth="100dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:scrollbars="none"
        android:stretchMode="columnWidth" >
    </GridView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingLeft="9dp"
        android:paddingRight="9dp"
        android:paddingTop="5dp" >

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:gravity="center"
             >
        </Button>
    </LinearLayout>

</LinearLayout>