3
回答
android中gallery空间问题

@红薯 你好,想跟你请教个问题:gallery滑到左右两边尽头时总是会留下一段空白区域,怎样才能让它不留空白呢?

举报
yafeng
发帖于5年前 3回/1K+阅
共有3个答案 最后回答: 5年前

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
   
   <Gallery
       android:id="@+id/gallery"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:gravity="fill_vertical"
       android:spacing="10dp"
       android:background="#0000FF"
       android:padding="0dp"
       />
  

</RelativeLayout>

 

代码

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryActivity extends Activity {
     Gallery gallery;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.garreyllayout);
        gallery = (Gallery)findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.pointToPosition(0, 0);
  
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
    class ImageAdapter extends BaseAdapter{

          private Context context;
          //图片源数组
          private Integer[] imageInteger={

                 R.drawable.member_login_choosed,
                 R.drawable.near_prefer_choosed,
                 R.drawable.prefer_nav_choosed,
                 R.drawable.sys_setting_choosed,
                 R.drawable.member_login_normal,
                 R.drawable.near_prefer_normal,
                 R.drawable.prefer_nav_normal,
                 R.drawable.sys_setting_normal
          };

          public ImageAdapter(Context c){

             context = c;

          }

          @Override

          public int getCount() {

             return imageInteger.length;

          }

          @Override

          public Object getItem(int position) {

             return position;

          }

          @Override

          public long getItemId(int position) {

             // TODO Auto-generated method stub

             return position;

          }

          @Override

          public View getView(int position, View convertView, ViewGroup parent) {

             ImageView imageView = new ImageView(context);

             imageView.setImageResource(imageInteger[position]);

             imageView.setScaleType(ImageView.ScaleType.FIT_XY);

              return imageView;

          }}


}

 

顶部