简体   繁体   English

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

[英]Android set fixed GridView height in custom Keyboard

I'm making a custom Emoji keyboard for a client. 我正在为客户制作自定义表情符号键盘。 We already have an iOS app and now trying to make something similar in android. 我们已经有一个iOS应用,现在尝试在android中制作类似的东西。 I'm using a gridView which is somewhat similar to the UICollectionView in iOS. 我正在使用gridView,它与iOS中的UICollectionView有点类似。 But I simply don't understand why I can't set a height for the gridView. 但是我根本不明白为什么我不能为gridView设置高度。 What I want to achieve is to set a specific keyboard height, like the same as system keyboard, so only 8-10 images appear and then I can scroll through the others. 我想要实现的是设置一个特定的键盘高度,与系统键盘一样,因此仅出现8-10张图像,然后可以滚动浏览其他图像。 Adding paging and horizontal scrolling would be a bonus and a final step. 添加分页和水平滚动将是一个奖励,也是最后一步。

Here is my XML file for the keyboard: 这是我的键盘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>

My InputServiceManagers onCreateInputView: 我的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: 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;
    }


}

This is how the end product should look like: 这是最终产品的外观: 在此处输入图片说明

And this is how it looks now: 这是现在的样子: 在此处输入图片说明

I Used your code and it's working fine, the stickers are showing in the keyboard area with a scrollbar. 我使用了您的代码,并且工作正常,标签在滚动条的键盘区域中显示。 By the way I'm working on a similar project right now, I think we can help each other if you want. 顺便说一下,我现在正在从事类似的项目,我想我们可以根据需要互相帮助。

Androi表情符号键盘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM