Android Layout Same Relative Size on ALL Screen Sizes

不羁岁月 提交于 2020-01-04 13:13:40

问题


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>

回答1:


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 :)



来源:https://stackoverflow.com/questions/11638197/android-layout-same-relative-size-on-all-screen-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!