【问题标题】:Layout margin top not working on two xml in Android studio布局边距顶部不适用于 Android Studio 中的两个 xml
【发布时间】:2022-10-19 04:51:32
【问题描述】:

我一直在输入和选择输入类型联系号码时布局没有自动滚动,我有两个xml文件,这个布局一个用于我的输入字段的正文,另一个是页脚有上一个和下一个按钮,我尝试在我的页脚 XML 中使用android:layout_marginTop="100dp" 但没有用,谁能帮我解决这个问题

正文.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/primary"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:layout_margin="15dp"
            android:textStyle="bold"
            android:text="I. Demographics"
            android:fontFamily="sans-serif-condensed"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="15dp" />
    </LinearLayout>

    <ScrollView
        android:background="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="660dp"
        android:fillViewport="true"
        >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="80dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:orientation="vertical">

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_hh_id"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginRight="10dp"
                    app:counterEnabled="true"
                    app:counterMaxLength="30"
                    app:endIconMode="clear_text"
                    app:errorEnabled="true"
                    app:helperText="Search Household ID!"
                    app:helperTextTextColor="@color/validation"
                    app:startIconDrawable="@drawable/ic_hh">

                    <EditText
                        android:id="@+id/edt_hh_id"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Household no."
                        android:inputType="text"
                        android:maxLength="30"
                        android:text="160310001-"

                        />
                </com.google.android.material.textfield.TextInputLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right">

                    <Button
                        android:id="@+id/btnSearchHh"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginRight="10dp"
                        android:padding="10dp"
                        android:text="Search"
                        android:textSize="12dp"
                        android:textStyle="bold" />
                </LinearLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_set"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/til_search"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginRight="10dp"
                    app:helperText="Required"
                    app:helperTextTextColor="@color/validation"
                    app:startIconDrawable="@drawable/ic_assigned">

                    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                        android:id="@+id/aat_set"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:hint="Set"
                        android:inputType="none" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_last_name"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/til_set"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:layout_marginRight="10dp"
                    app:endIconMode="clear_text"
                    app:helperText="Required"
                    app:helperTextTextColor="@color/validation"
                    app:startIconDrawable="@drawable/ic_person">

                    <EditText
                        android:id="@+id/edt_last_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Last Name:"
                        android:inputType="textMultiLine" />

                </com.google.android.material.textfield.TextInputLayout>


                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_first_name"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/til_last_name"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:layout_marginRight="10dp"
                    app:endIconMode="clear_text"
                    app:helperText="Required"
                    app:helperTextTextColor="@color/validation"
                    app:startIconDrawable="@drawable/ic_lastname">

                    <EditText
                        android:id="@+id/edt_first_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="First name"
                        android:inputType="textMultiLine" />

                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_middle_name"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/til_first_name"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:layout_marginRight="10dp"
                    app:endIconMode="clear_text"
                    app:startIconDrawable="@drawable/ic_middle">

                    <EditText
                        android:id="@+id/edt_middle_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Middle name"
                        android:inputType="textMultiLine" />
                </com.google.android.material.textfield.TextInputLayout>

       
                
                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_relationship_to_grantee"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/til_middle_name"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginRight="10dp"
                    app:helperText="Required"
                    app:helperTextTextColor="@color/validation"
                    app:startIconDrawable="@drawable/ic_baseline_escalator_warning_24">

                    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                        android:id="@+id/aat_relationship_to_grantee"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:hint="Relationship to Grantee" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_contact_no"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/til_relationship_to_grantee"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginRight="10dp"
                    app:counterEnabled="true"
                    app:counterMaxLength="10"
                    app:endIconMode="clear_text"
                    app:helperText="Required"
                    app:helperTextTextColor="@color/validation">

                    <EditText
                        android:id="@+id/edt_contact_no"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:drawableStart="@drawable/icon_contacts"
                        android:drawableLeft="@drawable/icon_contacts"
                        android:drawablePadding="6dp"
                        android:hint="Contact No."
                        android:inputType="number"
                        android:maxLength="10" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_contact_no_of"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/til_contact_no"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginRight="10dp"
                    app:helperText="Required"
                    app:helperTextTextColor="@color/validation"
                    app:startIconDrawable="@drawable/ic_baseline_person_24">

                    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                        android:id="@+id/aat_contact_no_of"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:hint="Contact No. of?" />
                </com.google.android.material.textfield.TextInputLayout>

          

            </LinearLayout>


        </RelativeLayout>
    </ScrollView>
</LinearLayout>

页脚.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Scanner.ScanCashCard">


        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:ignore="MissingConstraints" />


    <LinearLayout
        android:layout_marginTop="100dp"
        android:id="@+id/layoutDots"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:background="@color/primary"
        app:layout_constraintBottom_toBottomOf="parent" />

    <TextView
        android:id="@+id/tvPrev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_marginBottom="25dp"
        android:text="PREV"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tvNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_marginBottom="25dp"
        android:text="NEXT"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:padding="5dp"
        app:layout_constraintBottom_toBottomOf="parent">
        <ImageView
            android:id ="@+id/imageIv"
            android:height ="60dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

更新在我的清单中,我添加了windowSoftInputMode,但它似乎不起作用

 <activity
   android:name=".Scanner.ScanCashCard"
   android:windowSoftInputMode="adjustResize|adjustPan"
   android:parentActivityName=".MainActivity"/>

【问题讨论】:

  • 需要帮助,有人知道吗?
  • 添加发生这种情况的活动的清单声明。
  • @TheLibrarian 你是什么意思?如何添加活动清单?
  • 在 AndroidManifest 中,您必须为发生这种情况的屏幕定义活动(将其添加到帖子中)。有一个 Activity 属性 windowSoftInputMode 可以潜在地解决这个问题。
  • @TheLibrarian 我尝试添加windowSoftInputMode 请看上面更新的代码,但结果是一样的,需要帮助

标签: java android xml android-studio mobile


【解决方案1】:
  1. 使用&lt;FrameLayout&gt;作为根标签来堆叠页脚布局和正文布局
  2. 使用&lt;include&gt; 标签将您的footer.xml 布局添加到body.xml
  3. android:layout_height="match_parent" 设置为&lt;ScrollView&gt;
  4. 不需要设置这个android:windowSoftInputMode="adjustResize|adjustPan"

    笔记:这只会使&lt;ScrollView&gt; 中的内容向上滚动,当软板出现时,其余布局将在屏幕上保持不变,我认为这是有道理的。忽略附加的 gif 中的图标和颜色。

    这是代码:觉得有帮助请点赞

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary"
    android:focusable="true"
    android:focusableInTouchMode="true">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:fontFamily="sans-serif-condensed"
                android:text="I. Demographics"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/white"
                android:textSize="15dp"
                android:textStyle="bold" />
        </LinearLayout>
    
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:fillViewport="true">
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="80dp"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:orientation="vertical">
    
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right">
    
                        <Button
                            android:id="@+id/btnSearchHh"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"
                            android:layout_marginRight="10dp"
                            android:padding="10dp"
                            android:text="Search"
                            android:textSize="12dp"
                            android:textStyle="bold" />
                    </LinearLayout>
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_hh_id"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="30dp"
                        android:layout_marginRight="10dp"
                        app:counterEnabled="true"
                        app:counterMaxLength="30"
                        app:endIconMode="clear_text"
                        app:errorEnabled="true"
                        app:helperText="Search Household ID!"
                        app:helperTextTextColor="@color/validation"
                        app:startIconDrawable="@drawable/ic_hh">
    
                        <EditText
                            android:id="@+id/edt_hh_id"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="Household no."
                            android:inputType="text"
                            android:maxLength="30"
                            android:text="160310001-"
    
                            />
                    </com.google.android.material.textfield.TextInputLayout>
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_set"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/til_search"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:layout_marginRight="10dp"
                        app:helperText="Required"
                        app:helperTextTextColor="@color/validation"
                        app:startIconDrawable="@drawable/ic_assigned">
    
                        <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                            android:id="@+id/aat_set"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:focusable="false"
                            android:focusableInTouchMode="false"
                            android:hint="Set"
                            android:inputType="none" />
                    </com.google.android.material.textfield.TextInputLayout>
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_last_name"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/til_set"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="15dp"
                        android:layout_marginRight="10dp"
                        app:endIconMode="clear_text"
                        app:helperText="Required"
                        app:helperTextTextColor="@color/validation"
                        app:startIconDrawable="@drawable/ic_person">
    
                        <EditText
                            android:id="@+id/edt_last_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="Last Name:"
                            android:inputType="textMultiLine" />
    
                    </com.google.android.material.textfield.TextInputLayout>
    
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_first_name"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/til_last_name"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="15dp"
                        android:layout_marginRight="10dp"
                        app:endIconMode="clear_text"
                        app:helperText="Required"
                        app:helperTextTextColor="@color/validation"
                        app:startIconDrawable="@drawable/ic_lastname">
    
                        <EditText
                            android:id="@+id/edt_first_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="First name"
                            android:inputType="textMultiLine" />
    
                    </com.google.android.material.textfield.TextInputLayout>
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_middle_name"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/til_first_name"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="15dp"
                        android:layout_marginRight="10dp"
                        app:endIconMode="clear_text"
                        app:startIconDrawable="@drawable/ic_middle">
    
                        <EditText
                            android:id="@+id/edt_middle_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="Middle name"
                            android:inputType="textMultiLine" />
                    </com.google.android.material.textfield.TextInputLayout>
    
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_relationship_to_grantee"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/til_middle_name"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:layout_marginRight="10dp"
                        app:helperText="Required"
                        app:helperTextTextColor="@color/validation"
                        app:startIconDrawable="@drawable/ic_baseline_escalator_warning_24">
    
                        <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                            android:id="@+id/aat_relationship_to_grantee"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:focusable="false"
                            android:focusableInTouchMode="false"
                            android:hint="Relationship to Grantee" />
                    </com.google.android.material.textfield.TextInputLayout>
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_contact_no"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/til_relationship_to_grantee"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:layout_marginRight="10dp"
                        app:counterEnabled="true"
                        app:counterMaxLength="10"
                        app:endIconMode="clear_text"
                        app:helperText="Required"
                        app:helperTextTextColor="@color/validation">
    
                        <EditText
                            android:id="@+id/edt_contact_no"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableStart="@drawable/icon_contacts"
                            android:drawableLeft="@drawable/icon_contacts"
                            android:drawablePadding="6dp"
                            android:hint="Contact No."
                            android:inputType="number"
                            android:maxLength="10" />
                    </com.google.android.material.textfield.TextInputLayout>
    
                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/til_contact_no_of"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/til_contact_no"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:layout_marginRight="10dp"
                        app:helperText="Required"
                        app:helperTextTextColor="@color/validation"
                        app:startIconDrawable="@drawable/ic_baseline_person_24">
    
                        <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                            android:id="@+id/aat_contact_no_of"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:focusable="false"
                            android:focusableInTouchMode="false"
                            android:hint="Contact No. of?" />
                    </com.google.android.material.textfield.TextInputLayout>
    
    
                </LinearLayout>
            </RelativeLayout>
        </ScrollView>
    </LinearLayout>
    <include
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/footer"/>
    </FrameLayout>
    

【讨论】:

    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多