如何在各种设备上保持Android布局的一致性? [英] How to keep Android Layout consistent across various devices?

查看:56
本文介绍了如何在各种设备上保持Android布局的一致性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手,我刚刚在Playstore上发布了一个应用程序,发现我放置在布局中的按钮很少出现在某些设备上.如何确保各种设备之间的一致性? 我应该避免使用相对布局. 这是在"Galaxy Tab",Nexus 4和5上显示创建帐户按钮"的布局,但在Galaxy Grand上却没有.

I am new to Android Programming, I just published an app on playstore and found out that few buttons that I put in my layout doesn't show up on some of the devices. How do I make sure it is consistent across various devices? Should I avoid using relative layout. Here is the layout where Create Account Button shows up on Galaxy Tab, Nexus 4, and 5 but not on 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>

推荐答案

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

Rethink your design and provide a delightful experience for your users.

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

You need to focus on some areas of variability in Android devices and how those variations affect the development and design of Android applications.

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

结论:

如果您要使用一种屏幕布局,则不要使用带有权重的线性布局. 官方示例:

if Your Are Concerning with one screen layout than use linear layout with weights. 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" />

否则将可滚动的布局(如listview卡视图)与每行的realitivelayout一起使用,因为这样可以更有效地进行布局.

else use scroll-able layouts like listview card view with realitivelayout for each row because it is more efficiently.

P.S.您在xml中设置了很多不必要的属性..我只是在几秒钟内完成了此操作,以帮助您.您应该阅读RelativeLayout和LinearLayout的工作方式. 有关更多知识,请阅读官方文件

P.S. You have a lot of unnecessary atributes set in xml.. I just did this in a few sec to help you. You should read how RelativeLayout and LinearLayout work. For More knowledge read official documents

这篇关于如何在各种设备上保持Android布局的一致性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆