如果活动是全屏,则滚动不起作用

时间:2016-06-07 12:13:42

标签: android android-scrollview

我有一个带滚动视图的登录屏幕,我将布局全屏设置为

if (VERSION.SDK_INT >= 21) {
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

问题是即使我有滚动视图,当键盘弹出时我也无法滚动。这是我的布局

<ScrollView
android:id="@+id/scrollView"
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/ooru"
android:fitsSystemWindows="false"
tools:context=".android.LoginActivity">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/branding_login"
        android:layout_width="170dp"
        android:layout_height="35dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="55dp"
        android:scaleType="centerInside"
        android:src="@drawable/branding"/>

    <ImageView
        android:id="@+id/logged_in_icon"
        android:layout_width="54dp"
        android:layout_height="54dp"
        android:layout_below="@id/branding_login"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="42dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_fan_icon"/>

    .... //More views

</RelativeLayout>

</ScrollView>

我尝试搜索问题,有些人建议添加android:windowSoftInputMode =“adjustResize”,它对我不起作用。

P.S:此活动的父主题是Theme.AppCompat.Light.NoActionBar

3 个答案:

答案 0 :(得分:0)

主要问题是您将屏幕设置为全屏模式。

View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

如果您将全屏设置为

View.SYSTEM_UI_FLAG_FULLSCREEN 

android:windowSoftInputMode="adjustResize".

然而,一个问题是它会显示操作栏。

答案 1 :(得分:0)

尝试

1。KeyboardUtil课程添加到您的代码中。

2. 将以下行添加到onCreate活动方法

new KeyboardUtil(this, contentView) //扩充布局并将其分配给contentView对象

<强> KeyboardUtil.java

/*
 * Copyright 2015 Mike Penz All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.materialdrawer.util;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by mikepenz on 14.03.15.
 * This class implements a hack to change the layout padding on bottom if the keyboard is shown
 * to allow long lists with editTextViews
 * Basic idea for this solution found here: http://stackoverflow.com/a/9108219/325479
 */
public class KeyboardUtil {
    private View decorView;
    private View contentView;

    public KeyboardUtil(Activity act, View contentView) {
        this.decorView = act.getWindow().getDecorView();
        this.contentView = contentView;

        //only required on newer android versions. it was working on API level 19
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void enable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void disable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }


    //a small helper to allow showing the editText focus
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            decorView.getWindowVisibleDisplayFrame(r);

            //get screen height and calculate the difference with the useable area from the r
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            //if it could be a keyboard add the padding to the view
            if (diff != 0) {
                // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                //check if the padding is 0 (if yes set the padding for the keyboard)
                if (contentView.getPaddingBottom() != diff) {
                    //set the padding of the contentView for the keyboard
                    contentView.setPadding(0, 0, 0, diff);
                }
            } else {
                //check if the padding is != 0 (if yes reset the padding)
                if (contentView.getPaddingBottom() != 0) {
                    //reset the padding of the contentView
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };


    /**
     * Helper to hide the keyboard
     *
     * @param act
     */
    public static void hideKeyboard(Activity act) {
        if (act != null && act.getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

参考文献:

  1. https://github.com/mikepenz/MaterialDrawer/issues/95

  2. KeyboardUtil

答案 2 :(得分:0)

对于那些来这里期待解决方案的人。

如果您想要一个没有SupportActionBar的登录屏幕,请动态隐藏它,不需要全屏主题。

@Override
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_yours);
    if (getSupportActionBar() != null)
        getSupportAction().hide();

    ...
        your onCreate code here
    ...
}

希望有帮助。