Android在自定义键盘中设置固定的GridView高度

时间:2017-03-07 15:29:56

标签: android gridview height

我为客户制作自定义表情符号键盘。我们已经有了一个iOS应用程序,现在尝试在android中做类似的东西。我使用的gridView有点类似于iOS中的UICollectionView。但我根本不明白为什么我不能为gridView设置高度。 我想要实现的是设置一个特定的键盘高度,就像系统键盘一样,所以只出现8-10个图像然后我可以滚动浏览其他图像。添加分页和水平滚动将是一个奖励和最后一步。

这是键盘的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboardView"
    android:layout_width="match_parent"
    android:layout_height="240dp"
    android:background="@android:color/white"
    android:orientation="horizontal">

    <GridView
        android:id="@+id/imageGridView"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

</LinearLayout>

我的InputServiceManagers onCreateInputView:

 public View onCreateInputView() {

        keyboardView = getLayoutInflater().inflate(R.layout.keyboard,null);

        List<Integer> imageArray = new ArrayList<>();
        for (int i = 1; i <= 20;i++){
            try {
                Class res = R.drawable.class;
                String imageName = "sticker" + String.valueOf(i);
                Field field = res.getField(imageName);
                int drawableID = field.getInt(null);
                imageArray.add(drawableID);
            } catch (Exception e) {
                Log.e("MyTag","Failure to get drawable id.",e);
            }
        }

        imageGrid = (GridView) keyboardView.findViewById(R.id.imageGridView);
        imageGrid.setNumColumns(4);
        ImageGridAdapter adapter = new ImageGridAdapter(this);
        adapter.imageArray = imageArray;
        imageGrid.setAdapter(adapter);


        return keyboardView;

    }

GridViewAdapter:

public class ImageGridAdapter extends BaseAdapter {

    private Context mContext;
    public List<Integer> imageArray;

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

    public int getCount() {
        return imageArray.size();
    }

    public Object getItem(int position) {
        return imageArray.get(position);
    }

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

    @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(120, 120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //imageView.setPadding(1, 1, 1, 1);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imageArray.get(position));
        return imageView;
    }


}

这是最终产品的样子: enter image description here

这就是它现在的样子: enter image description here

1 个答案:

答案 0 :(得分:1)

我使用了你的代码并且工作正常,贴纸在键盘区域显示滚动条。顺便说一下,我现在正在开展一个类似的项目,如果你愿意,我想我们可以互相帮助。

Androi Emoji Keyboard