在所有屏幕尺寸的Andr​​oid布局相同的相对大小 [英] Android Layout Same Relative Size on ALL Screen Sizes

查看:130
本文介绍了在所有屏幕尺寸的Andr​​oid布局相同的相对大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一直唠叨我几个星期,现在出了问题,什么都试过,但无济于事。

我想要做的是对每个屏幕尺寸完全相同的布局。让我使用图片说明:

在一个2.7英寸设备:

在一个10.1英寸设备(用于说明目的严重Photoshop处理图片):

但与目前的Andr​​oid实现,我得到这个在10.1英寸的设备:

总之,我希望我的应用程序看起来是一样的(相对而言),包括按钮大小,文字缩放等等等等所有的屏幕尺寸。我这样做之前,通过重写视图类,使我自己的看法和内部绘制一切,但与该方法,添加视图元素得到笨拙柠柠快,有一个onclick事件回调功能的优势也就随之消失了(因为我只是编程提请被覆盖的视图类中的位图作为按钮的功能)。有什么办法来实现这个使用了Android的xml文件?

下面是我的(测试)XML code:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_vertical
    机器人:比重=中心>    <按钮
        机器人:ID =@ + ID / button_1
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文字=ABCDEF/>
    <按钮
        机器人:ID =@ + ID / button_2
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文字=GHI/>
    <按钮
        机器人:ID =@ + ID / button_3
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文字=JKLM/>
< / LinearLayout中>


解决方案

  IF((getResources()getConfiguration()屏幕布置和放大器; Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_LARGE) {
        Toast.makeText(这一点,大屏,Toast.LENGTH_LONG).show();    }
    否则如果((getResources()getConfiguration()屏幕布置和放大器; Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_NORMAL){
        Toast.makeText(这一点,正常大小的屏幕,Toast.LENGTH_LONG).show();    }
    否则如果((getResources()getConfiguration()屏幕布置和放大器; Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_SMALL){
        Toast.makeText(这一点,小尺寸屏幕,Toast.LENGTH_LONG).show();
    }
    其他{
        Toast.makeText(这一点,屏幕尺寸既不大,正常或小,Toast.LENGTH_LONG).show();
    }

我认为这可以帮助你,使你的应用程序看起来在通过这个code所有的屏幕分辨率片断,可以缩放控件编程,根据各自的THR屏幕尺寸相似。

感谢:)

A problem that has been nagging me for weeks now, tried everything but to no avail.

What I want to do is have the EXACT SAME layout on EVERY screen size. Let me illustrate by using a picture:

On a 2.7 inch device:

On a 10.1 inch device (badly photoshopped image for illustration purposes):

But with the current Android implementation, i get this on 10.1 inch devices:

In short, i want my application to look the same (relatively speaking) including button sizes, text scaling etc etc on all screen sizes. I did this before by overriding View class, making my own View and drawing everything inside, but with that method, adding view elements got unwieldly verry verry fast and the advantages of having an "onClick" callback functionality was also gone (since i just programatically drew bitmaps inside the overriden View class to function as buttons). Is there any way to achieve this using the android xml files?

Here is my (test) XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center">

    <Button
        android:id="@+id/button_1"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="ABCDEF" />
    <Button
        android:id="@+id/button_2"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="GHI" />
    <Button
        android:id="@+id/button_3"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="JKLM" />
</LinearLayout>

解决方案

if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {     
        Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();

    }
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
        Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();

    } 
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
        Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
    }
    else {
        Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
    }

I think this can help you to make your app look similar in all screen resolution through this code snippet you can Scale the Controls programmatically in accordance with thr respective Screen Size.

Thanks :)

这篇关于在所有屏幕尺寸的Andr​​oid布局相同的相对大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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