【问题标题】:Android Layout Same Relative Size on ALL Screen SizesAndroid 布局在所有屏幕尺寸上的相对尺寸相同
【发布时间】:2012-07-24 19:43:45
【问题描述】:

一个困扰我好几个星期的问题,尝试了所有方法,但无济于事。

我想做的是在每个屏幕尺寸上都有完全相同的布局。让我用一张图来说明:

在 2.7 英寸设备上:

在 10.1 英寸设备上(出于说明目的,图片经过严重 PS 处理):

但使用当前的 Android 实现,我在 10.1 英寸设备上得到了这个:

简而言之,我希望我的应用程序在所有屏幕尺寸上看起来都一样(相对而言),包括按钮大小、文本缩放等。我之前通过重写 View 类,制作自己的 View 并在里面绘制所有内容来做到这一点,但是使用这种方法,添加视图元素变得非常快速,并且具有“onClick”回调功能的优势也消失了(因为我只是以编程方式在覆盖的 View 类中绘制位图以用作按钮)。有没有办法使用android xml文件来实现这一点?

这是我的(测试)XML 代码:

<?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>

【问题讨论】:

标签: android layout size screen


【解决方案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();
    }

我认为这可以帮助您通过此代码使您的应用在所有屏幕分辨率上看起来都相似 sn-p 您可以根据各自的屏幕尺寸以编程方式缩放控件。

谢谢:)

【讨论】:

  • 好吧,这看起来像是一个妥协和大量的工作。我会把这段代码剪掉顺便说一句?在某处的应用程序活动中?我假设我必须遍历所有按钮和元素并手动更改按钮等上的文本大小?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多