繁体   English   中英

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

[英]Android Single Layout to FIt All Screen Sizes

我正在更新我的Android应用程序,并意识到我已经为每个可能的屏幕大小创建了一个布局(布局小,布局大,等等)。浏览每个XML文件并手动制作一个小的文件将是一个巨大的痛苦更改。 我正在尝试创建一个XML文件来支持所有屏幕大小。 在回顾了有关stackoverflow的Android文档和其他问题之后,似乎LinearLayout是最佳选择,因为您可以为布局中的每个项目提供weightSum和layout_weight。 这没有按预期工作(参见下面的代码和图像)。 我正确地这样做了吗? 我是否必须回去为每个可能的屏幕尺寸创建RelativeLayout?

我的图像是一个可绘制的文件夹,我的布局在一个布局文件夹中。

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>

结果视图(项目溢出和布局与屏幕尺寸不一致)

Nexus One:

在此输入图像描述

片剂:

在此输入图像描述

编辑:我添加了以下drawable -____文件夹。 它产生相同的结果。

在此输入图像描述

您可能需要考虑创建兼容性布局文件夹。 :)

在此输入图像描述

是的,我们通过使用android的百分比布局有相同的解决方案,我们现在可以使用app:layout_heightPercentapp:layout_widthPercent来适应所有使用单一布局的屏幕。

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

为什么现在使用LinearLayout weight属性我们有更简单的解决方案。

在这里演示!

GitHub项目在这里!

考虑一个简单的样本

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

在此输入图像描述

希望它对某人有用:-)

使用下面的布局来排列ImageButton和TextView。 它适用于所有屏幕尺寸的布局。

<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)可以是按钮,图像按钮,textview等所有内容......

这里有两个问题,一个是填充屏幕尺寸,另一个是支持各种分辨率尺寸的手机。 即使在xxxhdpi内,也有各种变化,因为新旗舰三星手机正在漂移至19.5 x 16。

线性布局和权重属性确实提供了良好的覆盖范围,但要注意嵌套标签和性能。 它对我处理的大多数情况都很有效。

此外,正如其他答案所指出的,标准尺寸的不同绘图/资源有助于在所有设备中保持相似的视图。

在此输入图像描述

暂无
暂无

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

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