简体   繁体   中英

Aligning view to center of screen in Android

I have stuck up with issue of placing scroll view at the middle of layout. When i see it on tablet the Scroll view has placed at top of screen. I need Scroll view to be placed at the center of screen (center_vertical|center_horizontal) and image should lies at (Center_vertical|top). Can any one guide me. how to implement it for all type of screen

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center_horizontal"
    tools:context=".SplashActivity" >

    <ImageView
        android:id="@+id/imgSplash"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal|top"
        android:layout_marginTop="20dp"
        android:contentDescription="@string/app_name"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher1" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imgSplash"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="45dp"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/mylay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/edittext_design"
                android:hint="Username"
                android:paddingLeft="10dp" />

            <EditText
                android:id="@+id/pass"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/edittext_design"
                android:hint="Password"
                android:paddingLeft="10dp" />

            <Button
                android:id="@+id/but"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/bottom_shape"
                android:text="Login" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
// try this way,hope this will help you...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center"
    tools:context=".SplashActivity"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imgSplash"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="20dp"
        android:contentDescription="@string/app_name"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher1" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/mylay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="20dp"
            android:gravity="center">

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/edittext_design"
                android:hint="Username"
                android:paddingLeft="10dp" />

            <EditText
                android:id="@+id/pass"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/edittext_design"
                android:hint="Password"
                android:paddingLeft="10dp" />

            <Button
                android:id="@+id/but"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/bottom_shape"
                android:text="Login" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center"
    android:layout_gravity="center"
    tools:context=".SplashActivity" >

     <ImageView
        android:id="@+id/imgSplash"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal|top"
        android:layout_marginTop="20dp"
        android:contentDescription="@string/app_name"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

    <RelativeLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:gravity="center"
    android:layout_gravity="center"
    tools:context=".SplashActivity" >



    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imgSplash"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="45dp"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/mylay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/edittext_design"
                android:hint="Username"
                android:paddingLeft="10dp" />

            <EditText
                android:id="@+id/pass"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/edittext_design"
                android:hint="Password"
                android:paddingLeft="10dp" />

            <Button
                android:id="@+id/but"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/bottom_shape"
                android:text="Login" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
</RelativeLayout>

Try That: Simply add relative layout;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
tools:context=".SplashActivity" >

<ImageView
    android:id="@+id/imgSplash"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal|top"
    android:layout_marginTop="20dp"
    android:contentDescription="@string/app_name"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_launcher1" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imgSplash"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="45dp"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/mylay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/edittext_design"
                android:hint="Username"
                android:paddingLeft="10dp" />

            <EditText
                android:id="@+id/pass"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/edittext_design"
                android:hint="Password"
                android:paddingLeft="10dp" />

            <Button
                android:id="@+id/but"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/bottom_shape"
                android:text="Login" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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