[英]How to change button size depending on screen size?
I have an image button. 我有一个图像按钮。 What is the way to change its size according to the screen size?
根据屏幕尺寸更改尺寸的方法是什么?
<Button
android:id="@+id/infobutton"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignLeft="@+id/button1"
android:layout_centerVertical="true"
android:layout_marginLeft="45dp"
android:background="@drawable/info_button" />
This answer is a bit involved, so bear with me. 这个答案有点牵强,所以请耐心等待。
You can create some methods like so.. 您可以创建一些类似的方法。
public static int getScreenDensity(Context context) {
int density = context.getResources().getDisplayMetrics().densityDpi;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
return DisplayMetrics.DENSITY_LOW;
case DisplayMetrics.DENSITY_MEDIUM:
return DisplayMetrics.DENSITY_MEDIUM;
case DisplayMetrics.DENSITY_HIGH:
return DisplayMetrics.DENSITY_HIGH;
case DisplayMetrics.DENSITY_XHIGH:
return DisplayMetrics.DENSITY_XHIGH;
case DisplayMetrics.DENSITY_XXHIGH:
return DisplayMetrics.DENSITY_XXHIGH;
case DisplayMetrics.DENSITY_XXXHIGH:
return DisplayMetrics.DENSITY_XXXHIGH;
default:
return DisplayMetrics.DENSITY_DEFAULT;
}
}
Then in an activity you can detect the screen size and adjust button's properties like so. 然后,在活动中,您可以检测屏幕尺寸并像这样调整按钮的属性。
private void setTunerPopoverSize( PopoverView popoverView ) {
int screenDensity = Util.getScreenDensity( this );
if (screenDensity == DisplayMetrics.DENSITY_HIGH) {
// set button size here if high density
} else if (screenDensity == DisplayMetrics.DENSITY_XHIGH) {
// set button size here if xhigh density
} else {
// set default button size
}
}
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.