Android 在自定义键盘中设置固定的 GridView 高度
Android set fixed GridView height in custom Keyboard
我正在为客户制作自定义表情符号键盘。我们已经有一个 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;
}
网格视图适配器:
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;
}
}
最终产品应该是这样的:
现在是这样的:
我使用了您的代码并且运行良好,贴纸显示在带有滚动条的键盘区域。顺便说一句,我现在正在做一个类似的项目,如果你愿意,我想我们可以互相帮助。
我正在为客户制作自定义表情符号键盘。我们已经有一个 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;
}
网格视图适配器:
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;
}
}
最终产品应该是这样的:
现在是这样的:
我使用了您的代码并且运行良好,贴纸显示在带有滚动条的键盘区域。顺便说一句,我现在正在做一个类似的项目,如果你愿意,我想我们可以互相帮助。