Android的ImageButton的尺寸不适合小屏幕设备上 [英] Android ImageButton Size doesn't fit on smaller screen device

查看:224
本文介绍了Android的ImageButton的尺寸不适合小屏幕设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何设置我的应用程序内容,以便他们可以固定在至少3.5英寸设备。

据看起来像这样在如此小尺寸的设备(4英寸)。

正如本图所示,所有四个图像按钮相互重叠&放大器;看在7英寸的屏幕不错。结果
有什么办法来解决这个问题?

这是我的布局:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@绘制/ BG2
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=StartActivity。>    <的ImageButton
        机器人:ID =@ + ID / imageButton1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentLeft =真
        机器人:背景=@空
        机器人:layout_marginLeft =24dp
        机器人:SRC =@绘制/ ABC/>    <的ImageButton
        机器人:ID =@ + ID / imageButton2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentRight =真
        机器人:背景=@空
        机器人:layout_marginRight =24dp
        机器人:SRC =@绘制/ nmbrs/>    <的ImageButton
        机器人:ID =@ + ID / imageButton3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentLeft =真
        机器人:背景=@空
        机器人:layout_marginLeft =24dp
        机器人:layout_marginBottom =10dp
        机器人:SRC =@绘制/ vegebtns/>    <的ImageButton
        机器人:ID =@ + ID / imageButton4
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentRight =真
        机器人:背景=@空
        机器人:layout_marginRight =24dp
        机器人:layout_marginBottom =10dp
        机器人:SRC =@绘制/ fruitsbtn/>< / RelativeLayout的>


解决方案

如果你想改变一些图像或大小根据屏幕的大小(不DPI),可以使用下面的方法:

  DisplayMetrics DM =新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(DM)。
双X = Math.pow(dm.widthPixels / dm.xdpi,2);
双Y = Math.pow(dm.heightPixels / dm.ydpi,2);
双screenInches =的Math.sqrt(X + Y);
如果(screenInches> sizeYouWant){
    //你想要做什么
}

I want to know how to set my application contents so that they can be fixed in at least 3.5 inch devices.

It is looking like this in such smaller size device (4 inches).

As shown in this image, all four image buttons are overlapping each other & look good in 7 inch screen.
Is there any way to fix this problem?

This is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg2"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StartActivity" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:background="@null"
        android:layout_marginLeft="24dp"
        android:src="@drawable/abc" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:background="@null"
        android:layout_marginRight="24dp"
        android:src="@drawable/nmbrs" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@null"
        android:layout_marginLeft="24dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/vegebtns" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@null"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/fruitsbtn" />

</RelativeLayout>

解决方案

If you want to change some image or size according to the size of the screen (not dpi), you can use the following method:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
if(screenInches > sizeYouWant){
    //do what you want
}

这篇关于Android的ImageButton的尺寸不适合小屏幕设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆