使图像适合GridView

时间:2017-07-23 15:02:30

标签: android image xamarin gridview

我想要适合我的4个gridview图像,如下图所示:

Desired layout

但在我目前的布局中,图像并不完全适合gridview。  有人可以帮忙吗?

目前的布局:

Current layout

Mainmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:background="@drawable/background">
    <include
        android:id="@+id/toolbar"
        layout="@layout/sublayout_Toolbar" />
    <android.support.v4.widget.DrawerLayout 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <FrameLayout android:id="@+id/frame_container"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="1">
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="0.4" />
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="0.6">
                <GridView
                    android:id="@+id/mainMenu_GridView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:numColumns="2"   
                    android:listSelector="@null"
                    android:verticalSpacing="0dp"
                    android:horizontalSpacing="0dp"/>
            </LinearLayout>
        </LinearLayout>
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            android:background="@color/light_blue" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

GridAdapter.MainMenu

namespace Dental_IT.Droid.Adapters
{
    class GridAdapter_MainMenu : BaseAdapter
    {
        private readonly Context context;
        private int numRows = 2;
        private int[] items;

        public GridAdapter_MainMenu(Context c, int[] i)
        {
            context = c;
            items = i;
            }

            public override int Count
            {
                get { return items.Length; }
            }

            public override Java.Lang.Object GetItem(int position)
            {
                return null;
            }

            public override long GetItemId(int position)
            {
                return 0;
            }

            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                ImageButton button;

                if (convertView == null)
                {
                    button = new ImageButton(context);

                    //LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
                    //button = inflater.Inflate(Resource.Layout.grid_mainmenu, null);
                    // ImageView imgView = button.FindViewById<ImageView>(Resource.Id.grid_image);

                    button.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                    button.SetMinimumHeight(Main_Menu.GRID_HEIGHT / numRows);
                    button.SetImageResource(items[position]);

                    switch (position)
                    {
                        case 0:
                            button.Click += delegate
                            {
                                Intent intent = new Intent(context, typeof(Select_Hospital));
                                context.StartActivity(intent);
                            };
                            break;

                        case 1:
                            button.Click += delegate
                            {
                                Intent intent = new Intent(context, typeof(My_Appointments));
                                context.StartActivity(intent);
                            };
                            break;
                        case 2:
                            button.Click += delegate
                            {
                                Intent intent = new Intent(context, typeof(Treatment_Information));
                                context.StartActivity(intent);
                            };
                            break;
                        case 3:
                            button.Click += delegate
                            {
                                Intent intent = new Intent(context, typeof(Search));
                                context.StartActivity(intent);
                            };
                            break;
                    }
                }
                else
                {
                    button = (ImageButton)convertView;
                }

                return button;
            }
        }
    }

MainMenu.cs

 private readonly int[] buttonTexts =
    {
        Resource.Drawable.ic_request_appt,
        Resource.Drawable.ic_my_appt,
        Resource.Drawable.ic_treatment_info,
        Resource.Drawable.ic_search
    };

1 个答案:

答案 0 :(得分:0)

对不起,我必须写这个作为答案,还不能发表评论。我假设其中一个父项传递了某种填充,所以我建议明确地为网格视图中的项目设置填充为0。如果这不起作用,则可能是继承的margin参数。否则可能是drawables文件夹中的图像尺寸不正确,因此重新调整到最佳状态。在我看来,无论如何他们可能会有所扩大。希望这会有帮助。如果您对我的评论/答案有任何疑问,请与我们联系。