我为客户制作自定义表情符号键盘。我们已经有了一个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;
}
}