2014-04-12 34 views
0

我正在研究Android的IME,爲此我創建了一個自定義視圖。IME被切斷的自定義Android視圖的一部分?

我設立的觀點在這樣一個XML文件:

<com.paldepind.mykeyboard.KeyboardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
/> 

我想的觀點是40%的屏幕的高度。因此,在我的自定義視圖我重寫onMeasure方法的觀點有:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int height = (int) (View.MeasureSpec.getSize(heightMeasureSpec) * .4); 
    setMeasuredDimension(widthMeasureSpec, 
         View.MeasureSpec.makeMeasureSpec(height, 
         View.MeasureSpec.EXACTLY)); 
} 

當手機上觀看的觀點正確始於60%下降在屏幕上 - 但它隨後被切斷還有更遠如下面的屏幕截圖所示。

Android screenshot

如果我更改了我的代碼.4以上只是1的觀點並填寫不過了整個屏幕的預期。看起來數字越低,視圖越多被切斷。

我真的不明白爲什麼這不起作用。任何幫助深表感謝!

回答

1

看來,onMeasure方法是由於某種原因開始調用兩次。第一次heightMeasureSpec參數等於屏幕尺寸,第二次將參數設置爲視圖之前的設置高度。因此,這個觀點最終只有40%的屏幕尺寸的40%。

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    int height = (int) (size.y * .4); 
    setMeasuredDimension(widthMeasureSpec, 
         View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)); 
} 

我已經在onMeasure方法調用獲取屏幕尺寸專門固定的問題

0

嘗試將keyboardview放入相對佈局中。也許可以解決它。