当 edittext 设置为 setVisible(true) 时,带有选项卡的操作栏会上升

Posted

技术标签:

【中文标题】当 edittext 设置为 setVisible(true) 时,带有选项卡的操作栏会上升【英文标题】:The actionbar with tabs goes up when edittext is setVisible(true) 【发布时间】:2018-07-26 03:40:41 【问题描述】:

片段中的“EditText”视图最初设置为 Visiblity.GONE。但是当用户从 d 下拉列表中选择一个项目时,我让它动态可见。它使带有标签的操作栏上升并且不会回来。如这些图片所示:

当不可见时(消失)。

可见时。

清单文件

<activity
    android:name=".LoanAppEducationActivity"
    android:label="@string/title_activity_loan_app_education"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar"
    android:windowSoftInputMode="adjustPan">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.mitron.mystro.EducationalLoansActivity" />
</activity>

activity_loan_app_education.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_
    android:layout_
    android:fitsSystemWindows="true"
    tools:context="com.mitron.mystro.LoanAppEducationActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:isScrollContainer="false"
        android:layout_
        android:layout_
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:isScrollContainer="false"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name"
            app:subtitle="@string/app_subtitle">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:isScrollContainer="false"
            app:tabBackground="@android:color/white"
            android:layout_
            android:layout_>

            <android.support.design.widget.TabItem
                android:id="@+id/personal_details"
                android:layout_
                android:layout_ />

            <android.support.design.widget.TabItem
                android:id="@+id/educational_details"
                android:layout_
                android:layout_ />

            <android.support.design.widget.TabItem
                android:id="@+id/loan_details"
                android:layout_
                android:layout_ />

            <android.support.design.widget.TabItem
                android:id="@+id/document_page"
                android:layout_
                android:layout_ />

        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <com.mitron.mystro.helper.view.MyViewPager
        android:id="@+id/container"
        android:isScrollContainer="false"
        android:layout_
        android:layout_
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

这是问题似乎所在的片段文件

导致问题的 EditText 位于已包含在此布局中的布局中。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/education_occupation_scroll_view"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:scrollbars="none"
    android:focusable="false"
    android:paddingBottom="16dp"
    android:isScrollContainer="false"
    tools:context="com.mitron.mystro.educational_loans.EduEducationOccupationFragment">

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_
            android:layout_
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <TextView
                android:layout_
                android:layout_
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:text="@string/educational_occupational_details"
                android:textSize="@dimen/form_edit_text_size"
                android:textStyle="bold" />
            <TextView
                android:layout_
                android:layout_
                android:gravity="end"
                android:text="@string/mandatory"/>
        </LinearLayout>

        <include layout="@layout/educational_detail_form" />

        <View
            android:layout_
            android:layout_
            android:layout_marginBottom="?attr/actionBarSize" />

    </LinearLayout>

</ScrollView>

包含的布局education_detail_form.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <TextView
        android:layout_
        android:layout_
        android:layout_marginBottom="0dp"
        android:layout_marginTop="4dp"
        android:text="@string/select_your_highest_qualification"
        android:textSize="@dimen/form_edit_text_size" />

    <com.jaredrummler.materialspinner.MaterialSpinner
        android:id="@+id/educational_detail_highest"
        android:layout_
        android:layout_
        android:layout_marginBottom="4dp"
        android:layout_marginTop="0dp"
        android:hint="@string/select_your_highest_qualification"
        android:textSize="@dimen/form_edit_text_size"
        app:ms_hint="@string/not_selected" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/other_qualification_layout"
        android:layout_
        android:layout_
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true" >

        <EditText
            android:id="@+id/other_qualification"
            android:layout_
            android:layout_
            android:layout_marginBottom="4dp"
            android:layout_marginTop="4dp"
            android:hint="@string/enter_your_highest_qualification"
            android:inputType="textPersonName|textAutoComplete"
            android:textSize="@dimen/form_edit_text_size" />

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

    <TextView
        android:layout_
        android:layout_
        android:layout_marginBottom="0dp"
        android:layout_marginTop="4dp"
        android:text="@string/select_your_current_occupation"
        android:textSize="@dimen/form_edit_text_size" />

    <com.jaredrummler.materialspinner.MaterialSpinner
        android:id="@+id/educational_detail_current_occupation"
        android:layout_
        android:layout_
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:hint="@string/select_your_current_occupation"
        android:textSize="@dimen/form_edit_text_size"
        app:ms_hint="@string/not_selected" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/academic_profile_recycler"
        android:layout_
        android:layout_
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp" />

    <LinearLayout
        android:id="@+id/salaried_layout"
        android:layout_
        android:layout_
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:layout_
            android:layout_>

            <EditText
                android:id="@+id/form_name_of_company"
                android:layout_
                android:layout_
                android:layout_marginBottom="4dp"
                android:layout_marginTop="4dp"
                android:hint="@string/name_of_the_company"
                android:inputType="textPersonName|textAutoComplete"
                android:textSize="@dimen/form_edit_text_size" />

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

        <android.support.design.widget.TextInputLayout
            android:layout_
            android:layout_>

            <com.mitron.mystro.helper.view.DateTextView
                android:id="@+id/form_working_since"
                android:layout_
                android:layout_
                android:hint="@string/working_since"
                android:textSize="@dimen/form_edit_text_size"
                app:dateCase="@integer/case_working_since" />

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

        <TextView
            android:layout_
            android:layout_
            android:layout_marginBottom="0dp"
            android:layout_marginTop="4dp"
            android:text="@string/net_monthly_income"
            android:textSize="@dimen/form_edit_text_size" />

        <EditText
            android:id="@+id/form_net_monthly_income"
            android:layout_
            android:layout_
            android:layout_marginBottom="4dp"
            android:layout_marginTop="0dp"
            android:hint="@string/fig_in_rupee"
            android:inputType="number"
            android:maxEms="10"
            android:maxLines="1"
            android:textSize="@dimen/form_edit_text_size" />

        <TextView
            android:layout_
            android:layout_
            android:layout_marginBottom="0dp"
            android:layout_marginTop="4dp"
            android:text="@string/monthly_obligations"
            android:textSize="@dimen/form_edit_text_size" />

        <EditText
            android:id="@+id/form_net_monthly_obligations"
            android:layout_
            android:layout_
            android:layout_marginBottom="4dp"
            android:layout_marginTop="0dp"
            android:hint="@string/fig_in_rupee"
            android:inputType="number"
            android:maxEms="10"
            android:maxLines="1"
            android:textSize="@dimen/form_edit_text_size" />

        <TextView
            android:layout_
            android:layout_
            android:layout_marginBottom="0dp"
            android:layout_marginTop="4dp"
            android:text="@string/office_address"
            android:textSize="@dimen/form_edit_text_size" />

        <include layout="@layout/address_lines" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

【参考方案1】:

从您的工具栏中删除此行:

 app:layout_scrollFlags="scroll|enterAlways"

完整代码:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:isScrollContainer="false"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="@string/app_name"
        app:subtitle="@string/app_subtitle">

通过 Java 代码删除:

Toolbar toolbar = findViewById(R.id.toolbar);  // or however you need to do it for your code
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);  // clear all scroll flags

【讨论】:

感谢 Abhishek kumar。 欢迎,快乐编码@SylvesterDas

以上是关于当 edittext 设置为 setVisible(true) 时,带有选项卡的操作栏会上升的主要内容,如果未能解决你的问题,请参考以下文章

SurfaceView setVisibility() 抛出 RuntimeException

setText 没有将文本设置为 EditText

为视图设置动画后 setVisibility 不起作用

为EditText设置nextFocus无效

EditText 填充在 Android 中无法按预期工作

ImageView 上的透明背景 EditText 不会显示光标