2011-02-17 57 views
0

我正在使用RelativeLayout將視圖定位在屏幕上的精確位置。這可以像預期的那樣使用一個視圖來繪製矩形。但是當使用像EditText這樣的Android視圖時,它們的繪製時間比大約8個單位的指定時間要短。點擊繪製的EditText之外(但是在由RelativeLayout指定的參數內),實際上會點擊EditText。Android視圖不像指定的高度

下面是一些代碼來說明我的意思:

package com.DrawDemo; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 

public class DrawDemo extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     RelativeLayout l = new RelativeLayout(this); 

     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100,100); 
     lp.leftMargin = 50; 
     lp.topMargin = 50; 

     DemoView demoview = new DemoView(this); 
     l.addView(demoview, lp); 

     EditText editText = new EditText(this); 
     l.addView(editText, lp); 

     setContentView(l); 
    } 

    private class DemoView extends View 
    { 
     public DemoView(Context context) 
     { 
      super(context); 
     } 

     @Override protected void onDraw(Canvas canvas) 
     { 
      super.onDraw(canvas); 

      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.FILL); 

      paint.setColor(Color.GREEN); 
      canvas.drawPaint(paint); 
     } 
    } 
} 

如果執行此,你會發現,EditText上明顯比矩形短。我嘗試過用onMeasure,setMinimumXXX,以及其他所有我能想到的東西。到目前爲止,只有在高度上添加8個左右的像素才能實現某種程度的成功(8似乎比嘗試使用高度的百分比效果更好)。

接下來的任務將是進入源代碼,但想知道是否有人已經解決了這個問題。

謝謝。

+0

你能提供截圖嗎?我無法在代碼中看到您實際嘗試完成的任務... – WarrenFaith 2011-02-17 21:08:15

+0

您可以嘗試http://img508.imageshack.us/i/screenht.jpg/。基本上,我使用完全相同的參數添加兩個視圖。一個視圖(爲了清晰起見,我爲了綠色而繪製的矩形)遵守參數。另一個視圖(EditText)比參數狀態短。有趣的是,我可以點擊EditText下方顯示的綠色部分,EditText就好像我點擊它一樣響應。所以也許我只需要擺弄利潤或什麼東西? – Darrell 2011-02-17 21:27:46

回答

0

這只是因爲使用了實際的EditText背景圖片。按鈕是相同的方式,由於某些原因谷歌決定繪製一些額外的透明填充像素9補丁背景圖像。真的,唯一的解決方案就是繪製自己的9貼片,或修改現有的Android 9貼片以去除額外的像素。

相關問題