Java Android:为每个设备自动调整大小
我创建了一个非常简单的应用程序,并将其下载到15个不同的设备中。对于其中一些布局,布局没有调整大小的问题,如下所示: 但当我在Sansung Galaxy Neo Plus和平板电脑Archos 101铜板上安装该应用程序时,这些物品的布局并不是我想要的位置。在平板电脑中,背景是不同颜色的视图,而在Galaxy Neo Plus中,则是这样的: 为了自动调整大小,在活动xml中,我使用了以下代码:Java Android:为每个设备自动调整大小,java,android,resize,Java,Android,Resize,我创建了一个非常简单的应用程序,并将其下载到15个不同的设备中。对于其中一些布局,布局没有调整大小的问题,如下所示: 但当我在Sansung Galaxy Neo Plus和平板电脑Archos 101铜板上安装该应用程序时,这些物品的布局并不是我想要的位置。在平板电脑中,背景是不同颜色的视图,而在Galaxy Neo Plus中,则是这样的: 为了自动调整大小,在活动xml中,我使用了以下代码: android:layout_marginTop="125dp" android:layout
android:layout_marginTop="125dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
从上/下/左/右设置按钮的边距
android:textSize="30dp"
设置按钮文本大小的步骤
布局代码如下所示:
<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
以下是布局的示例:
当然,你应该为每个按钮使用@string/
,以使你的应用程序可翻译,但这种基本布局应该让你正确了解如何制作一个正确缩放的布局
基本上,我使用一些线性布局,只说明视图应该如何排列(水平或垂直)。然后,我告诉包含三个主按钮的视图,其高度应为零(仅为了性能),最重要的部分是布局权重为1(android:layout\u weight=“1”
),它告诉系统将开放空间拆分为具有权重属性的所有视图。因此,这一个将尽可能填满您的显示。在我的案例中,其他视图仍将按您的需要呈现包裹内容以下是布局的示例:
当然,你应该为每个按钮使用@string/
,以使你的应用程序可翻译,但这种基本布局应该让你正确了解如何制作一个正确缩放的布局
基本上,我使用一些线性布局,只说明视图应该如何排列(水平或垂直)。然后,我告诉包含三个主按钮的视图,其高度应为零(仅为了性能),最重要的部分是布局权重为1(android:layout\u weight=“1”
),它告诉系统将开放空间拆分为具有权重属性的所有视图。因此,这一个将尽可能填满您的显示。在我的案例中,其他视图仍将按您的需要呈现包装内容
请添加您的布局。通常,您应该使用带有权重的线性布局来解决此问题。可能您没有使用正确的密度桶。平板电脑上的图片与手机上的图片有很大的不同。@rekire我添加了我的布局,但只是通过链接,因为我没有足够的声誉直接发布图片。你可以将它们添加为文本;-)最好edit@rekire我添加布局代码请添加您的布局。通常,您应该使用带有权重的线性布局来解决此问题。可能您没有使用正确的密度桶。平板电脑上的图片与手机上的图片有很大的不同。@rekire我添加了我的布局,但只是通过链接,因为我没有足够的声誉直接发布图片。你可以将它们添加为文本;-)最好edit@rekire我添加了布局代码为什么在第三个布局中使用android.support.v4.widget.Space作为权重?在第二部分中,您是否将权重放在布局总体礼仪中?sace小部件的用途是什么?我忘了提到这基本上是一个不会显示的占位符,但它需要一个房间才能让您在右边和左边各有一个视图。这是得到这个结果的最快的方法。如果我想从第二个布局和imageview中去掉边距呢?我能用marginTop吗?或者线性布局中还有其他功能?如果您熟悉,基本上可以像CSS一样使用填充和边距。请记住,当您想要一个不同的值时,您必须编写4个属性,而不是一个(padding vs paddingTop、paddingButtom、paddingLeft和paddingRight)。确定padding有效,但将margintop放入(例如,在第二个布局中),它没有改变什么---编辑---没有,好的,它可以工作我没有看到你写的已经存在的边距项目:)你为什么在第三个布局中使用android.support.v4.widget.Space作为权重?在第二部分中,您是否将权重放在布局总体礼仪中?sace小部件的用途是什么?我忘了提到这基本上是一个不会显示的占位符,但它需要一个房间才能让您在右边和左边各有一个视图。这是得到这个结果的最快的方法。如果我想从第二个布局和imageview中去掉边距呢?我能用marginTop吗?或者线性布局中还有其他功能?如果您熟悉,基本上可以像CSS一样使用填充和边距。请记住,当您想要一个不同的值时,您必须编写4个属性,而不是一个(padding vs paddingTop、paddingButtom、paddingLeft和paddingRight)。确定padding有效,但将margintop放入(例如,在第二个布局中),它不改变什么---编辑---不好,它工作了我没有看到你写的已经存在的空白项目:)
<?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>