简体   繁体   English

Android单一布局以获取所有屏幕尺寸

[英]Android Single Layout to FIt All Screen Sizes

I am updating my Android app and realized that I have created a layout for every possible screen size (layout-small, layout-large, etc...) It would be a huge pain to go through every XML file and manually make a small change. 我正在更新我的Android应用程序,并意识到我已经为每个可能的屏幕大小创建了一个布局(布局小,布局大,等等)。浏览每个XML文件并手动制作一个小的文件将是一个巨大的痛苦更改。 I am attempting to create a single XML file to support all screen sizes. 我正在尝试创建一个XML文件来支持所有屏幕大小。 After reviewing the Android documentation and other questions on stackoverflow, it seems LinearLayout is the best choice as you can provide a weightSum and layout_weight for each item in the layout. 在回顾了有关stackoverflow的Android文档和其他问题之后,似乎LinearLayout是最佳选择,因为您可以为布局中的每个项目提供weightSum和layout_weight。 This is not working as expected (see below code and images). 这没有按预期工作(参见下面的代码和图像)。 I am doing this correctly? 我正确地这样做了吗? Do I have to go back to creating a RelativeLayout for every possible screen size? 我是否必须回去为每个可能的屏幕尺寸创建RelativeLayout?

My images are an a single drawable folder and my layouts are in a single layout folder. 我的图像是一个可绘制的文件夹,我的布局在一个布局文件夹中。

XML XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:gravity="center"
    android:orientation="vertical"
    android:weightSum="100" >

    <ImageButton
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/image0"
        android:background="@null"
        android:layout_weight="30" />

    <ImageButton
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"  
        android:scaleType="fitCenter"
        android:src="@drawable/image1"
        android:background="@null"
        android:layout_weight="30" />

    <ImageButton
        android:id="@+id/key"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:background="@null"
        android:src="@drawable/image0_key" />

    <TextView
        android:id="@+id/tvScore"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Score: 0"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_weight="10"
        android:layout_gravity="left" />

</LinearLayout>

Resulting View (overflow of items and layout not consistent for screen sizes) 结果视图(项目溢出和布局与屏幕尺寸不一致)

Nexus One: Nexus One:

在此输入图像描述

Tablet: 片剂:

在此输入图像描述

EDIT: I have added the following drawable-____ folders. 编辑:我添加了以下drawable -____文件夹。 It produces the same result. 它产生相同的结果。

在此输入图像描述

You might want to consider creating compatibility layout folders. 您可能需要考虑创建兼容性布局文件夹。 :) :)

在此输入图像描述

Yes we have a solution for the same by using android's percent layout we can now use app:layout_heightPercent and app:layout_widthPercent to fit all the screens using single layout. 是的,我们通过使用android的百分比布局有相同的解决方案,我们现在可以使用app:layout_heightPercentapp:layout_widthPercent来适应所有使用单一布局的屏幕。

compile 'com.android.support:percent:23.0.0'

Why use LinearLayout weight property now we have simpler solution. 为什么现在使用LinearLayout weight属性我们有更简单的解决方案。

Demo HERE ! 在这里演示!

GitHub project HERE ! GitHub项目在这里!

Consider a simple sample 考虑一个简单的样本

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/fifty_huntv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff7acfff"
        android:text="20% - 50%"
        android:textColor="@android:color/white"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@id/fifty_huntv"
        android:background="#ffff5566"
        android:text="80%-50%"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="50%"
        />

</android.support.percent.PercentRelativeLayout>

在此输入图像描述

Hope it's useful for someone :-) 希望它对某人有用:-)

Use Below layout for arranging your ImageButton and TextView. 使用下面的布局来排列ImageButton和TextView。 It works for all screen size Layouts. 它适用于所有屏幕尺寸的布局。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3" />

<ImageButton
    android:id="@+id/imageBtn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/imageBtn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/ic_launcher" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Score: 0" />

</LinearLayout>

永远不要把重量总和像百,只要尝试使用一位数

DisplayMetric dm =new DisplayMetric();

getWindowManager().getDefaultDisplay().getMetrics(dm);
int h= dm.heightPixels;
int w= dm.widthPixels;
h=h/10; // 10 is for example for 10% of display height
w=((w*20)/100); // 20%of display width
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(w,h);
YourObject.setLayoutParams(params);

//(YourObject) can be everything such as button , image button, textview,... //(YourObject)可以是按钮,图像按钮,textview等所有内容......

There are two issues here, one is to fill the size of the screen and the other is supporting the various resolution sizes of mobiles. 这里有两个问题,一个是填充屏幕尺寸,另一个是支持各种分辨率尺寸的手机。 Even within xxxhdpi, there are variations as new flagship Samsung Mobiles are drifting to 19.5 x 16. 即使在xxxhdpi内,也有各种变化,因为新旗舰三星手机正在漂移至19.5 x 16。

Linear layout along with weight attributes does give a good coverage but beware of the nested tags and performance. 线性布局和权重属性确实提供了良好的覆盖范围,但要注意嵌套标签和性能。 It worked out well for most of the scenarios I have handled. 它对我处理的大多数情况都很有效。

In addition, as pointed out in other answers, different drawables/resources for the standard sizes helps maintaining similar view in all devices. 此外,正如其他答案所指出的,标准尺寸的不同绘图/资源有助于在所有设备中保持相似的视图。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM