安卓:对齐的LinearLayout中心所有屏幕尺寸 [英] Android: Align LinearLayout center for all screen sizes

查看:115
本文介绍了安卓:对齐的LinearLayout中心所有屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个布局我的登录屏幕,我尝试以下codeS。使用一些硬codeD值我得到了一些屏幕尺寸我中心的绿色布局中心。我知道使用硬codeD值在不同的屏幕尺寸不会工作。我试了如此众多的东西。请提供给我的最好办法

 <?XML版本=1.0编码=UTF-8&GT?;<滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@绘制/背景><的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < RelativeLayout的
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / register_header
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =50dp
        机器人:方向=横向>        < ImageView的
            机器人:ID =@ + ID / title_image
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =40dp
            机器人:layout_centerHorizo​​ntal =真
            机器人:layout_centerVertical =真
            机器人:layout_marginBottom =5DP
            机器人:layout_marginTop =5DP
            机器人:比重=中心
            机器人:SRC =@绘制/ logo_blue/>
    < / RelativeLayout的>    <的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
         机器人:layout_marginLeft =150dp
        机器人:layout_marginRight =150dp
        机器人:layout_marginTop =50dp
        机器人:layout_marginBottom =50dp
        机器人:方向=垂直>        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID /的UserInfo
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ + ID / WelcomeInfo
            机器人:重力=center_vertical
            机器人:方向=横向>            <的TextView
                机器人:ID =@ + ID /用户
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:文字=用户名
                机器人:文字颜色=#000000
                机器人:TEXTSIZE =10PT
                机器人:文字样式=大胆/>            <的EditText
                机器人:ID =@ + ID /用户名
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:提示=输入用户名
                机器人:单线=真
                机器人:文字颜色=#000000>
            < /&的EditText GT;
        < / LinearLayout中>        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / PassInfo
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ ID /的UserInfo
            机器人:方向=横向>            <的TextView
                机器人:ID =@ + ID /密码
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:文字=密码
                机器人:文字颜色=#000000
                机器人:TEXTSIZE =10PT
                机器人:文字样式=大胆/>            <的EditText
                机器人:ID =@ + ID /密码
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:提示=Enterpassword
                机器人:单线=真
                机器人:文字颜色=#000000/>
        < / LinearLayout中>        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / StoreNumInfo
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ + ID / WelcomeInfo
            机器人:重力=center_vertical
            机器人:方向=横向>            <的TextView
                机器人:ID =@ + ID / NUM
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:文字=门店数量
                机器人:文字颜色=#000000
                机器人:TEXTSIZE =10PT
                机器人:文字样式=大胆/>            <的EditText
                机器人:ID =@ + ID / store_num
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:提示=输入店号
                机器人:imeOptions =actionDone
                  机器人:密码=真                机器人:文字颜色=#000000>
            < /&的EditText GT;
        < / LinearLayout中>        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID /选项
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ ID / PassInfo
            机器人:方向=横向>            <复选框
                机器人:ID =@ + ID / signed_in
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:文字=保持登录状态下
                机器人:文字颜色=#000000/>
            <按钮
                机器人:ID =@ + ID /登入
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:layout_weight =1
                机器人:文字=登陆
                机器人:TEXTSIZE =28dp
                机器人:文字样式=大胆/>
        < / LinearLayout中>
    < / LinearLayout中>
< / LinearLayout中> < /滚动型>



解决方案

使用您的绿色布局RelativeLayout的封面。然后定义greenLayout这样。
的android:layout_centerHorizo​​ntal =真正的
的android:layout_centerVertical =真正的

看我的XML就可以集中所有的屏幕

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直><的LinearLayout
    机器人:ID =@ + ID / lin_all
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:方向=垂直>
< / LinearLayout中>< / RelativeLayout的>

I want to design a layout for my login screen, I tried the following codes. Using some hardcoded values I got my center green color layout center for some screen sizes. I know using hardcoded values wont work on different screen sizes. I tried numerous stuffs on SO. Please provide me the best way

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/register_header"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/title_image"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:src="@drawable/logo_blue" />


    </RelativeLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         android:layout_marginLeft="150dp"
        android:layout_marginRight="150dp"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp"
        android:orientation="vertical" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/UserInfo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/WelcomeInfo"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/user"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="User Name"
                android:textColor="#000000"
                android:textSize="10pt"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/username"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:hint="Enter username"
                android:singleLine="true"
                android:textColor="#000000" >
            </EditText>
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/PassInfo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/UserInfo"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/pwd"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Password"
                android:textColor="#000000"
                android:textSize="10pt"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/password"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:hint="Enterpassword"
                android:singleLine="true"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/StoreNumInfo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/WelcomeInfo"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/num"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Store Number"
                android:textColor="#000000"
                android:textSize="10pt"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/store_num"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:hint="Enter the Store number"
                android:imeOptions="actionDone"
                  android:password="true"

                android:textColor="#000000" >
            </EditText>
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/Options"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/PassInfo"
            android:orientation="horizontal" >

            <CheckBox
                android:id="@+id/signed_in"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Stay Signed-in"
                android:textColor="#000000" />


            <Button
                android:id="@+id/login"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Login"
                android:textSize="28dp"
                android:textStyle="bold" />
        </LinearLayout>


    </LinearLayout>
</LinearLayout> </ScrollView>


解决方案

Use your cover of your green layout as RelativeLayout. and then define your greenLayout like this. android:layout_centerHorizontal="true" android:layout_centerVertical="true"

Look at my xml it can center all screen

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/lin_all"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical" >
</LinearLayout>

</RelativeLayout>

这篇关于安卓:对齐的LinearLayout中心所有屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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