سؤال

I have a simple GridView. The following is the XML

        <LinearLayout
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical" >

       <GridView
            android:id="@+id/grid_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numColumns="7"
            android:verticalSpacing="2dp"
            android:rotationY="180" >
        </GridView>
    </LinearLayout>

I create a TextView and insert it as an item in the gridview. Basically the idea is to create a customized calendar.

In the adaptor I have

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView = new TextView(mContext);
    textView.setText(days.get(position).toString());
    textView.setRotationY(180);
    textView.setGravity(Gravity.CENTER);

    textView.setBackground(mContext.getResources().getDrawable(R.drawable.grey_box));

    int x = mContext.getResources().getDimensionPixelSize(R.dimen.calendar_slot);
    textView.setLayoutParams(new GridView.LayoutParams(x, x));
    return textView;
}

R.dimen.calendar_slot equals to 30dp.

What I fail to understand is , given the above why does my gridview appear like below ? . I need the columns to be merged together. But they have spaces between them.

Can anyone aid ? Screenshot

هل كانت مفيدة؟

المحلول

The reason why there is a lot of space between the columns of gridview is that the textview in your layout is not occupying the complete column space provided by the grid view

To avoid this problem , you have to calculate the device's screen width and height and divide it by number of columns and rows respectively. This will give you the exact width and height you needed for your single textview.Set this as the dimensions of your text view. You will get equal space between your rows and columns

The code will be as follows

    DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
    screen_width=displayMetrics.widthPixels;    //width of the device screen
    screen_height=displayMetrics.heightPixels;   //height of device screen

     int view_width=screen_width/columns;   //width for text view
     int view_height=screen_height/rows;   //height for text view

     textview.getgetLayoutParams().width=view_width;
     textview.getgetLayoutParams().height=view_height;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top