2017-03-07 76 views
0

我正在爲客戶端自定義Emoji鍵盤。我們已經有一個iOS應用程序,現在試圖在android中做出類似的東西。我使用的是一個類似於iOS中的UICollectionView的gridView。但我根本不明白爲什麼我不能爲gridView設置高度。 我想要實現的是設置特定的鍵盤高度,就像系統鍵盤一樣,所以只顯示8-10張圖像,然後我可以滾動瀏覽其他圖像。添加分頁和水平滾動將是一個獎勵和最後一步。Android設置固定GridView自定義鍵盤高度

下面是鍵盤我的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

+0

我懷疑你的部分代碼沒有顯示你的問題。如果我膨脹你的佈局文件,我得到一個240dp的高度,但在顯示器的頂部。查看keyboard.xml的父級。 – Gary99

回答

1

我用了你的代碼,它的做工精細,貼都出現在滾動條的鍵盤區域。順便說一句,我現在正在做一個類似的項目,如果你願意,我想我們可以互相幫助。

Androi Emoji Keyboard

+0

謝謝,我找到了解決方案! :) – user3621033

+0

給我發電子郵件:[email protected] – user3621033

+0

嗨@ user3621033,我正在開發一個像你這樣的項目,你能跟我分享你的代碼嗎?我的代碼出錯了。謝謝 –