簡體   English   中英

在Android中將視圖與屏幕中心對齊

[英]Aligning view to center of screen in Android

我一直堅持將滾動視圖放在布局的中間的問題。 當我在平板電腦上看到它時,“滾動”視圖已放在屏幕頂部。 我需要將滾動視圖放置在屏幕的中心(center_vertical | center_horizo​​ntal),圖像應位於(Center_vertical | top)。 誰能指導我。 如何在所有類型的屏幕上實現它

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>

嘗試這個

<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>

試試看:只需添加相對布局即可;

<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