简体   繁体   English

设备上的布局看起来有所不同

[英]Layout looks different on devices

Hi My app looks different on different devices. 嗨,我的应用在不同的设备上看起来有所不同。 I use Samsung Galagy S2 as my testing device, but when I tried install app to another device Samsung Galaxy Note 3. 我使用Samsung Galagy S2作为测试设备,但是当我尝试将应用程序安装到另一台设备Samsung Galaxy Note 3上时。

Display resolution: Samsung galaxy S2: 800x480(hdp) Samsung galaxz Note 3: 1920x1080(xxhdp) 显示分辨率:Samsung galaxy S2:800x480(hdp)Samsung galaxz Note 3:1920x1080(xxhdp)

My app looks on: Samsung galaxy S2 like THIS Samsung galaxy Note 3 like THIS 我的应用程序看起来:三星Galaxy S2像三星Galaxy Note 3像

As you can see in first picture it looks good but on Note 3 there are a lot of empty space at bottom of app. 如您在第一张图片中所见,它看起来不错,但是在Note 3上,应用程序底部有很多空白空间。 All images I use are xxhdp and hdp too. 我使用的所有图像也是xxhdp和hdp。 The problem is in gap between buttons. 问题出在按钮之间。 I set it in dp so I thought in every device it will look like in my phone. 我在dp中进行了设置,因此我认为在每台设备中它的外观都将在手机中显示。 But on galaxy S2 and galaxy Note 3 it looks same gap. 但是在星系S2和星系注3上,它看起来是相同的。

This is my layout. 这是我的布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayoutHA"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background" >

    <RelativeLayout
        android:id="@+id/relativeLayoutHA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/infoButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/info"
            />

        <Button
            android:id="@+id/facebookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/infoButton"
            android:layout_marginRight="5dp"
            android:background="@drawable/facebook"
             />

         <Button
            android:id="@+id/emailButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/facebookButton"
            android:layout_marginRight="5dp"
            android:background="@drawable/email"
             />

    </RelativeLayout>

    <Button
        android:id="@+id/animal"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/animal_pic"
        android:text="Animals" />

    <Button
        android:id="@+id/insect"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/insect"
        android:text="Insects" />

    <Button
        android:id="@+id/nature"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/nature"
        android:text="Nature" />

    <Button
        android:id="@+id/people"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/people"
        android:text="People" />

    <Button
        android:id="@+id/gun"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/gun"
        android:text="Guns" />



</LinearLayout>

您应使用LinearLayout覆盖所有垂直按钮,并将此LinearLayout的layout_height设置为match_parent。在此LinearLayout内,将每个按钮的layout_weight设置为相同的值(例如:1),并将layout_height设置为0dp

您必须使用dimen管理高度和宽度。为values-v11,values-v14,values-sw600dp值布局创建尺寸,并根据布局和检查设置高度和宽度。

I also had same issue, but i solved it by creating 3 different layouts for same activity. 我也遇到了同样的问题,但是我通过为同一活动创建3种不同的布局来解决了这个问题。

  1. layout_large(for xhdp layouts) layout_large(用于xhdp布局)
  2. layout_xlarge(for xxhdp layouts) layout_xlarge(用于xxhdp布局)
  3. layout(for xhdp layouts) 布局(用于xhdp布局)

You dont need to bother about assigning preferred layout, android device select the layout which is most suitable for their screen. 您无需为分配首选布局而烦恼,Android设备会选择最适合其屏幕的布局。 My answer seem to be extra work but its most preferrable if you want to have consistent view on different screen size. 我的回答似乎是多余的工作,但如果您希望在不同的屏幕尺寸上获得一致的视图,则它是最可取的。 Read documentation . 阅读文档

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

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