簡體   English   中英

在ScrollView / LinearLayout的底部放置一個視圖

[英]Place a View at Bottom in ScrollView/LinearLayout

我剛開始使用Android,並且正在開發我的第一個包含登錄屏幕的應用程序。

最初,我曾使用RelativeLayoutAdView或使用android:layout_alignParentBottom="true"將其他任何View正確放置在父視圖的底部。

然后,在閱讀有關材料指南時,我指的是在ScrollView中使用LinearLayout的XML。 現在,我無法將AdView放到最底端(嘗試將XML導入到Android Studio中)。 在這里搜索答案,我也將其放置在RelativeLayout並再次使用android:layout_alignParentBottom="true" ,但未成功。 我知道Android用戶已經使用了所有這些功能,希望他們能為我提供幫助。

因此,列出我的基本問題為:

  • 您能告訴我OP將ScrollView用於登錄屏幕活動的實際使用嗎? 如果您知道如何處理活動的UI,是否可以在任何提示中張貼兩行或更多行,以提示我應該使用什么? 任何提示將不勝感激。
  • 主要問題-那么,如何將AdView置於底部? 一個解釋將不勝感激。

謝謝。 :)

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#FFB74D">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="15dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:backgroundTintMode="src_atop">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="Gyaanify"
                android:textSize="55sp"
                android:textColor="#FF3D00"
                android:textStyle="normal|bold"
                android:textAllCaps="false"
                android:textAlignment="center"
                android:fontFamily="cursive"
                android:layout_margin="10dp" />

        </RelativeLayout>

        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/etUserName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="Email" />
        </android.support.design.widget.TextInputLayout>

        <!-- Password Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="Password"/>
        </android.support.design.widget.TextInputLayout>

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="24dp"
            android:padding="20dp"
            android:text="Login"
            android:onClick="OnLogin"
            android:background="#EF6C00"
            android:textColor="@android:color/background_light" />

        <TextView android:id="@+id/link_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="No account yet? Create one"
            android:gravity="center"
            android:textSize="16dip"
            android:layout_marginTop="40dp" />

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_register"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="24dp"
            android:text="Register @ Gyaanify"
            android:onClick="OnRegister"
            android:padding="20dp"
            android:layout_marginTop="10dp"
            android:background="#EF6C00"
            android:textColor="@android:color/background_light" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:orientation="vertical"
            android:layout_alignParentBottom="true">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>
        </RelativeLayout>

    </LinearLayout>
</ScrollView>

每當您使用應填充整個屏幕的ScrollView ,它都應具有android:layout_width="match_parent" android:layout_height="match_parent"屬性。 這在您的代碼中是正確的。 但是內部的LinearLayout應該具有android:layout_width="match_parent" android:layout_height="wrap_content" ,如果您想到了的話,這很有意義。

現在,要將AdView放置在屏幕底部並使所有內容滾動到屏幕上方,您需要以下布局結構:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <!-- all your content here -->

        </LinearLayout>
    </ScrollView>

    <com.google.android.gms.ads.AdView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

ScrollView權重設置為1可確保您的AdView固定在底部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM