首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android:自动调整每个设备的大小

Android:自动调整每个设备的大小
EN

Stack Overflow用户
提问于 2016-04-08 18:11:30
回答 1查看 127关注 0票数 1

我已经创建了一个非常简单的应用程序,我下载它在15个不同的设备。对于其中一些人,布局没有调整大小的问题,如下所示:

应该在所有布局中的布局

但当我在三星银河Neo Plus和平板电脑Archos 101铜板中安装应用程序时,这些项目的布局并不像我想要的那样。在平板电脑的背景上有不同的颜色视图,而在Galaxy中是这样的:银河Neo Plus的布局

为了自动调整大小,在auto xml中,我使用了这些代码:

代码语言:javascript
运行
复制
android:layout_marginTop="125dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"

从上/下/左/右设置按钮的页边距

代码语言:javascript
运行
复制
android:textSize="30dp"

设置按钮文本的大小

布局代码如下:

代码语言:javascript
运行
复制
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Al meglio di:"
    android:textSize="22dp"
    android:id="@+id/almegliodi"
    android:layout_alignParentTop="true"
    android:layout_marginTop="220dp"
    android:layout_centerHorizontal="true"
    android:typeface="serif"
    android:textStyle="bold|italic"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" UNO "
    android:textStyle="bold"
    android:textSize="35dp"
    android:textColor="@android:color/white"
    android:id="@+id/uno"
    android:background="@android:drawable/dialog_holo_dark_frame"
    android:onClick="uno"
    android:layout_below="@+id/almegliodi"
    android:layout_marginTop="30dp"
    android:layout_centerHorizontal="true"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" TRE "
    android:textStyle="bold"
    android:textSize="35dp"
    android:textColor="@android:color/white"
    android:id="@+id/tre"
    android:background="@android:drawable/dialog_holo_dark_frame"
    android:onClick="tre"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/uno"
    android:layout_marginTop="30dp"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" CINQUE "
    android:textStyle="bold"
    android:textSize="35dp"
    android:textColor="@android:color/white"
    android:id="@+id/cinque"
    android:background="@android:drawable/dialog_holo_dark_frame"
    android:onClick="cinque"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/tre"
    android:layout_marginTop="30dp"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ESCI"
    android:textStyle="bold"
    android:textSize="30dp"
    android:id="@+id/esci"
    android:background="@android:color/transparent"
    android:layout_marginBottom="7dp"
    android:layout_marginLeft="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:onClick="esci"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="INDIETRO"
    android:textStyle="bold"
    android:textSize="30dp"
    android:textColor="@android:color/white"
    android:id="@+id/indietro"
    android:background="@android:color/transparent"
    android:onClick="indietro"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_alignTop="@+id/esci"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="7dp"
    />
</RelativeLayout

我在谷歌上搜索,但我发现使用'dp‘是自动调整所有设备的页边距和文本大小的最佳方法。

我的错误是什么?我要怎么做才能解决兼容性问题?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-08 18:28:37

下面是一个布局的示例:

代码语言:javascript
运行
复制
<?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="#005"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal"
        android:src="@android:drawable/btn_star_big_on"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="24dp"
        android:layout_weight="1"
        android:background="#cfff"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>

        <android.support.v4.widget.Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>
    </LinearLayout>
</LinearLayout>

您应该使用@string/为每个按钮,以使您的应用程序可翻译,但这个基本的布局应该给您正确的想法,如何作出正确的布局,以正确的规模。

基本上,我使用一些线性布局,只是说视图应该如何安排(水平的或垂直的)。然后,我告诉视图,它包含三个主按钮,它的高度应该是零(仅为性能考虑),最重要的部分是布局权重为1 (android:layout_weight="1"),它告诉系统将开放空间分割为带有权重属性的所有视图。所以这个会尽可能地填满你的展示。在我的示例wrap_content中,其他视图仍将按您所需的方式呈现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36506505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档