简体   繁体   English

布局对齐问题

[英]Alignment issue with layout

I have basic layout for a login screen on my app. 我在应用程序上具有登录屏幕的基本布局。 I want to align the logo at the top of the screen. 我想将徽标对准屏幕顶部。 The email & password fields, & login button in the middle of the screen. 屏幕中间的电子邮件和密码字段以及登录按钮。 Than the two buttons create account & forgot password at the bottom of the screen (stacked on top of each other). 比这两个按钮在屏幕底部创建帐户和忘记密码(彼此堆叠)。 I can't figure out for the life of me how to do it. 我一生都想不出该怎么做。 I tried to use "aligninParent" attribute which did work, but when I changed the phone orientation it caused the fields to overlap. 我尝试使用确实起作用的“ aligninParent”属性,但是当我更改电话方向时,导致字段重叠。 Would I need to create a different layout for landscape? 我是否需要为景观创建其他布局?

My Code: 我的代码:

<?xml version="1.0" encoding="utf-8"?><!-- Login Form -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="@drawable/radialback">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="10dp">
    <!-- Logo -->
        <ImageView
            android:id="@+id/imgLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="25dp"
            android:contentDescription="@string/se_logo"
            android:scaleType="fitCenter"
            android:src="@drawable/se_logo" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_vertical">
         <!-- Email Address -->
            <EditText
                android:id="@+id/login_email"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:layout_marginBottom="15dp"
                android:hint="@string/email"
                android:textColorHint="@color/blue"
                android:singleLine="true"
                android:background="@drawable/blue_edit_text">

                <requestFocus />
            </EditText>

       <!-- Password -->
            <EditText
                android:id="@+id/login_password"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:layout_marginBottom="10dp"
                android:hint="@string/pw"
                android:textColorHint="@color/blue"
                android:password="true"
                android:singleLine="true"
                android:background="@drawable/blue_edit_text" />

        <!-- Login Button -->
            <Button
                android:id="@+id/btnLogin"
                android:layout_width="210dp"
                android:layout_height="35dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/btn_style"
                android:layout_gravity="center"
                android:text="@string/login"
                android:textColor="#ffffff" />


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

          <!-- Link to Register -->
                <Button
                    android:id="@+id/btnlink_to_register"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dip"
                    android:layout_marginBottom="5dp"
                    android:text="@string/create_account"
                    android:textColor="@color/text_color"
                    android:background="@null"
                    android:textSize="15sp" />

             <!-- Forgot Password Button -->
                <Button
                    android:id="@+id/btnlink_forgot_password"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="@string/forgot_password"
                    android:textColor="@color/text_color"
                    android:background="@null"
                    android:textSize="12sp" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

</ScrollView>

Don't use fill_parent, replace for match_parent (the former is deprecated). 不要使用fill_parent,而要替换match_parent(不推荐使用前者)。

Short answer: yes, you need different layouts for landscape if you want it to look differently. 简短的回答:是的,如果您希望其外观不同,则需要使用不同的布局。

You seem to be using tooooo many layouts. 您似乎在使用许多布局。

The Logo shouldn't be inside a linear layout, you can use AlignParentTop=true for that image view. 徽标不应位于线性布局内,可以将AlignParentTop = true用于该图像视图。

The next LinearLayout (for email/user, etc.) should be "alignBelow= 下一个LinearLayout(用于电子邮件/用户等)应为“ alignBelow =

Add IDs to your views/widgets if you use Relative Layouts, because that's how you position them in the screen. 如果您使用相对布局,则将ID添加到视图/小部件中,因为这是将它们放置在屏幕上的方式。

Only use LinearLayouts to put individual groups of views together in a single block that can adapt as a whole. 仅使用LinearLayouts将单个视图组放到一个可以整体适应的块中。

Read a little bit about layout weight to have one of your views use the remaining "unused" space accordingly. 阅读一些有关布局权重的信息,以使您的视图之一相应地使用剩余的“未使用”空间。

ANd post a screenshot of what you want :) 并张贴您想要的屏幕截图:)

Try this.. 尝试这个..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/login_iv"
    android:layout_width="400dp"
    android:layout_height="125dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="150dp"
    android:src="@drawable/yourlogo" />

<RelativeLayout
   android:id="@+id/rl_login"
   android:layout_width="fill_parent"
   android:layout_below="@+id/login_iv"
   android:layout_height="fill_parent"       
   android:gravity="center_horizontal">

    <EditText
        android:id="@+id/etName"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_marginTop="50dp"
        android:hint="email" />



    <EditText
        android:id="@+id/etPass"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etName"
        android:layout_marginTop="15dp"
        android:password="true"
        android:hint="password"
        android:singleLine="true"
        android:focusableInTouchMode="true"/>



    <Button
        android:id="@+id/bLogin"
        android:layout_width="350dp"
        android:layout_height="45dp"
        android:layout_below="@+id/etPass"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:onClick="login"
        android:text="LOGIN"
        android:textColor="#FFFFFF"
        android:textSize="15sp" />
</RelativeLayout>

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

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