【问题标题】:How to keep Android Layout consistent across various devices?如何在各种设备上保持 Android 布局一致?
【发布时间】:2015-03-30 04:11:24
【问题描述】:

我是 Android 编程新手,我刚刚在 Playstore 上发布了一个应用程序,发现我放在布局中的几个按钮没有显示在某些设备上。如何确保它在各种设备上保持一致? 我应该避免使用相对布局吗? 这是创建帐户按钮在 Galaxy Tab、Nexus 4 和 5 上显示但在 Galaxy Grand 上不显示的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/background">

    <RelativeLayout android:id="@+id/container" android:layout_width="match_parent"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="20dp"
        android:layout_height="match_parent"
        android:background="#85000000">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:orientation="vertical"
            android:layout_centerHorizontal="true"
            android:id="@+id/linearLayout">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="95dp"
                android:layout_marginBottom="10dp"
                android:hint="@string/email"
                android:textColor="#fd9a22"
                android:textCursorDrawable="@drawable/cursor_color"
                android:textColorHint="#ffffff"
                android:id="@+id/txtUser"
                android:background="@drawable/edit_text"
                android:drawableLeft="@drawable/dr_email"
                android:drawablePadding="10dp"
                android:paddingLeft="-3dp"
                android:singleLine="true"
                android:layout_gravity="center_horizontal" />

            <EditText
                android:layout_width="match_parent"
                android:inputType="textPassword"
                android:layout_height="wrap_content"
                android:id="@+id/txtPwd"
                android:hint="@string/pwd"
                android:textColor="#fd9a22"
                android:textCursorDrawable="@drawable/cursor_color"
                android:textColorHint="#ffffff"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:background="@drawable/edit_text"
                android:drawableLeft="@drawable/dr_pwd"
                android:drawablePadding="10dp"
                android:layout_gravity="center_horizontal" />

            <Button
                android:layout_width="match_parent"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content"
                android:text="@string/sign_in"
                android:id="@+id/btnSignIn"
                android:background="@drawable/ainovatheme_btn_default_holo_light"
                android:onClick="login"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#fd9a22"
                android:background="#85000000"
                android:id="@+id/txtLoginErr"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/forgot_pwd"
                android:textColor="#ffffff"
                android:id="@+id/btnForgotPwd"
                android:layout_gravity="center_horizontal"
                android:onClick="forgotPassword"
                style="?android:attr/borderlessButtonStyle"/>

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="3dp"
                android:background="#d4dce9" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:text="@string/create_account"
                android:textColor="#fd9a22"
                android:id="@+id/btnCreateAccountActivity"
                android:onClick="createAccount"
                style="?android:attr/borderlessButtonStyle"
                android:layout_gravity="center_horizontal" />


        </LinearLayout>


    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

标签: android user-interface android-linearlayout android-relativelayout


【解决方案1】:

重新思考您的设计并为您的用户提供令人愉悦的体验。

您需要关注 Android 设备的某些可变性领域,以及这些变化如何影响 Android 应用程序的开发和设计。

  1. 布局位置
  2. 大小、填充和边距
  3. 常用布局
  4. 资源限定符
  5. 拆分视图

结论:

如果您关注的是一种屏幕布局,而不是使用带权重的线性布局。 OFFICIAL Example:

<?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:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/to" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/subject" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="top"
    android:hint="@string/message" />
<Button
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="@string/send" />

否则使用可滚动的布局,如列表视图卡片视图和每行的 realitivelayout,因为它更有效。

附:您在 xml 中设置了很多不必要的属性。我只是在几秒钟内完成了此操作以帮助您。您应该阅读 RelativeLayout 和 LinearLayout 如何工作。 更多知识请阅读official documents

【讨论】:

    【解决方案2】:

    我建议您阅读以下页面:http://www.google.com/design/spec/layout/metrics-keylines.html

    您有一个设计指南来创建适用于台式机、平板电脑和手机的应用程序。

    关于代码,您只需要为每个设备(通常是平板电脑和手机,或者添加电视和穿戴设备)创建布局。

    【讨论】:

      【解决方案3】:

      您可以使用 PercentRelativeLayout,您可以在其中使用百分比设置位置、大小和边距。检查下面的链接。

      http://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-14
        • 2022-10-04
        • 1970-01-01
        • 2014-12-07
        相关资源
        最近更新 更多