2012-01-31 46 views
0

我設計了一個側邊欄,其中包含26個垂直字符。設計的文字在onDraw()中根據需要使用畫布來支持所有屏幕

當我使用HVGA屏幕(模擬器)時,它適合屏幕,但它不適合其他屏幕。我在onDraw()方法中使用畫布設計了側邊欄。

如何將此邊欄適用於所有類型的屏幕?

private void init() { 
    float GESTURE_THRESHOLD_DP = 13.9f; 
    // Get the screen's density scale 
     float scale = getResources().getDisplayMetrics().density; 
    // Convert the dps to pixels, based on density scale 
    mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f); 
    // Use mGestureThreshold as a distance in pixels... 
    l = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 
      'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 
    setBackgroundColor(0x44FFFFFF); 
}  
public SideBar(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
} 
public void setListView(ListView _list) { 
    list = _list; 
    sectionIndexter = (SectionIndexer) _list.getAdapter(); 
} 
public boolean onTouchEvent(MotionEvent event) { 
    super.onTouchEvent(event);   
    int i = (int) event.getY(); 
    System.out.println("i is:"+i); 
    int idx = i/m_nItemHeight;  
    if (idx >= l.length) { 
     idx = l.length - 1;   
    } else if (idx < 0) {   
     idx = 0; 
    } 
    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {   
     if (sectionIndexter == null) {    
      sectionIndexter = (SectionIndexer) list.getAdapter(); 
     } 
     int position = sectionIndexter.getPositionForSection(l[idx]);   
     if (position == -1) {  
      return true; 
     }   
     list.setSelection(position); 
    } 
    return true; 
} 
protected void onDraw(Canvas canvas) { 
    Paint paint = new Paint(); 
    float f; 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(18); 
    float scale = (float) getWidth(); 
    System.out.println(""+Paint.FAKE_BOLD_TEXT_FLAG); 
    paint.setTextAlign(Paint.Align.CENTER); 
    paint.setAntiAlias(true); 
    float widthCenter = getMeasuredWidth() /2; 
    for (int i = 0; i < l.length; i++) { 
     canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint); 
    } 
    super.onDraw(canvas); 
} 
} 

回答

0

發佈您的onDraw()會有所幫助。無論如何,您必須使用getHeight()/26作爲適合所有屏幕的參考。看來你採取了不斷的措施。