多列上的Android间距

时间:2013-10-11 13:03:10

标签: android relativelayout

我正在尝试将所有图像水平均匀地放置在其下方的评级开始,但我正在努力做到这一点。有人能告诉我吗?

public class LevelActivity extends Activity {

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

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(LevelActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.level, menu);
        return true;
    }


    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

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

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

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

        // create a new ImageView for each item referenced by the Adapter
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                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;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.zero, R.drawable.one,
                R.drawable.two, R.drawable.three,
                R.drawable.four, R.drawable.five,
                R.drawable.zero, R.drawable.one,
                R.drawable.two, R.drawable.three,
                R.drawable.four, R.drawable.five,
                R.drawable.zero, R.drawable.one,
                R.drawable.two, R.drawable.three,
                R.drawable.four, R.drawable.five,
                R.drawable.zero, R.drawable.one
        };
    }
}

enter image description here 新的布局,现在只需要明星。

2 个答案:

答案 0 :(得分:1)

使用gridview

 <GridView
                android:id="@+id/list_view"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:columnWidth="90dp"
                android:gravity="center"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:scrollingCache="false"
                android:animationCache="false"
                android:fastScrollEnabled="true"
                android:verticalSpacing="10dp" />

答案 1 :(得分:0)

试试这个,它可以帮到你

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/levelselection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:background="@null"
            android:src="@drawable/icon" />

         <RatingBar
             android:id="@+id/rtbProductRating"
             style="@style/RatingBar" // I dont know what you are using this line
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_below="@+id/levelselection"
             android:numStars="3"
             android:rating="2"
             android:paddingTop="20sp" />

    </RelativeLayout>