Screen resolution [closed]
Actually i have created an array of buttons,now my question is whether it will be fit in all screen resolution or not..as i have use nexus s device and the here its working fine..i mean to say that all the buttons are arranged in perfect order..more ever when i placed background image to each buttons its work fine..now i doubt whether it works for cheaper device by the same code i have written or i have to do something extra for that.thats means whether it fit for all screen resolution..or not..the code i have written is..
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
//Create Button
for (i = 0; i<6; i++){
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout,param);
for(j=0;j<7;j++){
pBtnDay[i][j]=new Button(this);
rowLayout.addView(pBtnDay[i][j],param);
pBtnDay[i][j].setOnLongClickListener(this);
pBtnDay[i][j].setOnClickListener(this);
}
}
return true;
one more thing when i try to find wi开发者_Go百科dth and height of the buttons it return 0..why?
Using weightSum is the best solution if you know exactly how many elements you want displayed on your screen, across multiple devices. So if it shows up on, say a HTC Wildfire(LDPI) then it will also show on your Nexus(HDPI).
As for your second question, I´m assuming you are using View.getHeight() and View.getWidth() to obtain these values. Since your buttons hasn´t actually been drawn yet, Android has not designated dimension values to them, that´s why they turn up 0. If you need the width and height of these buttons in onCreate, then you need to do some math of your own :)
One of the basic philosophy in Adnroid Application is portability across platforms with different Screen Resolution, pixel depth and Oreintation.
How to achieve this, has been explained well in this link. Enjoy!
In case you are interested in getting Screen Parameters, then you can use DisplayMetrics and Window Manager. Shash
For the second answer: if you need the button dimension values, use:
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
if (hasFocus)
{
// calculates dimesions
}
}
In fact, as told before, in the onCreate you still have not calculate them.
精彩评论