Android - 如何修复First Item始终与Left和Last Item对齐始终与Right Gallery对齐

时间:2013-12-11 03:11:14

标签: android gallery

默认情况下,图库将第一个元素和最后一个元素对齐到中心。

我试过android gallery image position problem。对于fisrt时间,第一项对齐左侧。没关系。但是当我滑动画廊时,第一个项目仍然有左边距离,最后一个项目仍然有向右间距。

image_gallery_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="1sp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="130dp"
    android:layout_height="110dp"
    android:layout_gravity="center" >
</ImageView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TextView>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<Gallery
    android:id="@+id/galleryid"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

private Activity activity;
private static LayoutInflater inflater = null;

public ImageAdapter(Activity a) {
    activity = a;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder {
    public TextView text;
    public ImageView image;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolder holder;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.image_gallery_items, null);
        holder = new ViewHolder();
        holder.text = (TextView) vi.findViewById(R.id.textView1);
        holder.image = (ImageView) vi.findViewById(R.id.image);
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();
    holder.text.setText(name[position]);
    final int stub_id = data[position];
    holder.image.setImageResource(stub_id);

    return vi;
}

private int[] data = { R.drawable.category, R.drawable.category_002,
        R.drawable.category_003, R.drawable.category_004,
        R.drawable.category_005, R.drawable.category_006,
        R.drawable.category_007, R.drawable.category_008,
        R.drawable.category_009, R.drawable.category_010 };

private String[] name = { "Image1", "Image2", "Image3", "Image4", "Image5",
        "Image6", "Image7", "Image8", "Image9", "Image10" };

}

MainActivity.java

public class MainActivity extends Activity {

private Gallery galleryView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    galleryView = (Gallery) findViewById(R.id.galleryid);
    galleryView.setAdapter(new ImageAdapter(this));
    galleryView.setSelection(1);

    galleryView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {

            Toast.makeText(MainActivity.this, "" + position,
                    Toast.LENGTH_SHORT).show();

        }
    });

}

}

我的问题是:如何将画廊的第一项始终固定为左,将最后一项固定为右,没有间距。

enter image description here

0 个答案:

没有答案