2011-07-21 65 views
0

我正在開發一個Android應用程序。我以編程方式創建視圖,並且對於此應用程序是必需的,因爲它應該是動態的。我可以將資源按照hdpi,mdpi和ldpi分配,但我想知道如何設置視圖的寬度和高度,以便在所有Android屏幕上顯示正確。目前我正在使用密度因子如下。如何創建將在Android的多個屏幕上運行的視圖?

ImageView imgMainPic = new ImageView(this); 
RelativeLayout.LayoutParams rllpImg = 
    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
            (int)(440*Global.nDensity)); 
rllpImg.addRule(RelativeLayout.ALIGN_TOP,mainRelativeLyt.getId()); 
imgMainPic.setId(1); 
imgMainPic.setLayoutParams(rllpImg);   
imgMainPic.setBackgroundResource(R.drawable.pic1);  
mainRelativeLyt.addView(imgMainPic); 

回答

0

根據設備計算視圖的寬度和高度並不是一個好習慣。最好的辦法是用小,大,超大等定義不同的屏幕不同的佈局。這裏是link約支持多種屏幕尺寸的Android開發者..

0

嘗試用使用WRAP_CONTENT的高度也

RelativeLayout.LayoutParams rllpImg = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT); 
相關問題