简体   繁体   English

在Android中将视图与屏幕中心对齐

[英]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). 我需要将滚动视图放置在屏幕的中心(center_vertical | center_horizo​​ntal),图像应位于(Center_vertical | top)。 Can any one guide me. 谁能指导我。 how to implement it for all type of screen 如何在所有类型的屏幕上实现它

layout.xml 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>

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

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