簡體   English   中英

如何在另一個布局的底部包括布局

[英]How to include layout at the bottom of another layout

step.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:id="@+id/stepperLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:ms_stepperType="dots" />

activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:id="@+id/ScrollView01"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingBottom="3dp"
                  android:paddingLeft="16dp"
                  android:paddingRight="16dp"
                  android:paddingTop="3dp">

                   <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/firstname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="11"
                        android:maxLines="1"
                        android:hint="First name"
                        android:textColor="#000000"
                        android:maxLength="15"
                        android:tag="@+id/first_name"
                        android:textCursorDrawable="@null" />

                </android.support.design.widget.TextInputLayout>

    </RelativeLayout>

    <include
            android:id="@+id/bottom_Menu"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/step"/>

    </ScrollView>

這是step.xml的設計

在此處輸入圖片說明

這是activity_main的設計。 在此,我包括了step.xml

在此處輸入圖片說明

我需要這樣的輸出。

在此處輸入圖片說明

當我嘗試在activity_main包含step.xml時,出現異常“滾動視圖只能承載一個子視圖”。 請幫我。

更改您的StepperLayout高度以包裝內容

step.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:id="@+id/stepperLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:ms_stepperType="dots" />

並如下更改activity_main布局:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/profile_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:windowSoftInputMode="adjustResize|adjustPan">


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

        <ScrollView
            android:id="@+id/layout_scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingBottom="70dp"
                android:paddingTop="3dp">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/firstname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="11"
                        android:maxLines="1"
                        android:hint="First name"
                        android:textColor="#000000"
                        android:maxLength="15"
                        android:tag="@+id/first_name"
                        android:textCursorDrawable="@null" />

                </android.support.design.widget.TextInputLayout>

            </RelativeLayout>


        </ScrollView>


    </LinearLayout>


    <RelativeLayout
        android:id="@+id/btn_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true">

        <include
            android:id="@+id/bottom_Menu"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/step"/>

    </RelativeLayout>

</RelativeLayout>

您應該在scrollview內生一個scrollview (正如錯誤所述)。 將所有子項都包裝在另一個(例如) LinearLayoutRelativeLayout並用wrap_content來獲取寬度和高度以及垂直方向。

Docs中 :ScrollView是一個FrameLayout,這意味着您應在其中放置一個包含要滾動的全部內容的子元素。 這個孩子本身可能是具有復雜對象層次結構的布局管理器。 經常使用的子項是在垂直方向上的LinearLayout,它顯示用戶可以滾動瀏覽的頂級項目的垂直數組。

EDIT最簡單的示例可以像這樣:

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
      <LinearLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
           // all the views currently in your ScrollView
     </LinearLayout>
</ScrollView>

StepperLayout高是匹配的父級,這就是為什么它處於最高位置,請更改您的StepperLayout高以包裝內容

step.xml

    <?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/stepperLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_stepperType="dots" />

並且ScrollView只有一個孩子,因此您的include布局移動相對布局內 ,並將android:fillViewport =“ true”添加到ScrollView

       <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:fillViewport="true"
    android:layout_height="fill_parent">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="3dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="3dp">

 <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <EditText
                        android:id="@+id/firstname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="11"
                        android:maxLines="1"
                        android:hint="First name"
                        android:textColor="#000000"
                        android:maxLength="15"
                        android:tag="@+id/first_name"
                        android:textCursorDrawable="@null" />
                </android.support.design.widget.TextInputLayout>
    <include
            android:id="@+id/bottom_Menu"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/step"/>
    </RelativeLayout>

    </ScrollView>

使用此代碼:滾動視圖標記結束后,添加步驟xml代碼,如下所示。

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

<LinearLayout
        android:id="@+id/buttons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_alignParentBottom="true">
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Custom Button1"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Custom Button2"/>
</LinearLayout>

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/buttons">
     <!--Scrollable content here-->
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">                
        <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="test text"
                android:textSize="40dp"/>
    </LinearLayout>
</ScrollView>
 <com.stepstone.stepper.StepperLayout 
 android:id="@+id/stepperLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:ms_stepperType="dots" />
</RelativeLayout>

暫無
暫無

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

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