Android中透明状态栏导致EditText被键盘遮挡。

4
我制作了一个Android应用程序,遵循Google的材料设计指南。因此,我实现了最新的支持库(我的minSdk值为14)。
现在,我面临一个问题:我已经将状态栏设置为透明,并使用自定义的ToolBar(在XML文件中定义)。一切都运行良好,除了来自https://github.com/makovkastar/FloatingActionButton的FloatingActionButton跳过了XML定义的布局边距。同样,我的应用程序UI之一使用TextView登录用户:一旦聚焦,它们就会被软键盘隐藏。
这两个问题出现在将主样式从“AppCompat.DarkActionBar”替换为“AppCompat.NoActionBar”之后(并将项目“android:windowTranslucentStatus”设置为“true”)。
我尝试在清单中使用windowSoftInputMode和“adjustPan | adjustResize”,但没有任何改变。在具有“EditText”的布局中添加“ScrollView”也无法解决我的问题。
我的英语很差,我知道图片胜过言语,所以这里是我想要做的事情以及发生的情况:

(图片链接)http://i.stack.imgur.com/fIRCl.png

有什么想法吗?谢谢阅读!


编辑:我已经解决了FloatingButton的问题,只需要在组件的XML声明中添加这一行代码:android:layout_alignParentBottom="true"

现在唯一的问题是键盘会隐藏EditText。以下是我的代码:

.

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp"
    tools:context=".MainActivity">

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

        <!-- My toolbar -->
        <include
            android:id="@+id/tool_bar"
            layout="@layout/toolbar_design">
        </include>

        <!-- Fragment's place -->
        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true">
        </FrameLayout> <!-- Open drawer bug fix -->

    </LinearLayout>

    <!-- Sliding Drawer -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:clipToPadding="false"
        android:overScrollFooter="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/drawer_background"/>

</android.support.v4.widget.DrawerLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/md_blue_800"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="4dp" >
</android.support.v7.widget.Toolbar>

fragment_profile.xml(带有EditText的视图)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/container_dropshadow">

        <!-- Top fragment's Image --> 
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/top_image"/>

    </LinearLayout>

    <!-- Scrollview in case of screen's height is too small -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:id="@+id/rlFillProfile">

            <!-- long long text ( 9 lines ) -->
            <TextView
                android:text="@string/text_edit_profile" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:id="@+id/tvEditProfile" />

            <!-- User ID EditText -->
            <com.rengwuxian.materialedittext.MaterialEditText
                android:layout_below="@id/tvEditProfile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:maxLines="1"
                android:singleLine="true"
                android:maxEms="10"
                android:hint="Your user ID"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:id="@+id/etUserID"
                android:layout_alignLeft="@+id/etUserPassword"
                android:layout_alignStart="@+id/etUserPassword" />

            <!-- User ID TextView -->
            <TextView
                android:id="@+id/tvUserID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Identifiant :"
                android:textSize="18dp"
                android:layout_marginBottom="16dp"
                android:layout_alignBottom="@id/etUserID"
                android:layout_alignRight="@+id/tvUserPassword"
                android:layout_alignEnd="@+id/tvUserPassword" />

            <!-- User password TextView -->
            <com.rengwuxian.materialedittext.MaterialEditText
                android:layout_below="@id/etUserID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:maxLines="1"
                android:singleLine="true"
                android:maxEms="10"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:inputType="textPassword"
                android:id="@+id/etUserPassword"
                android:hint="******"
                android:layout_toRightOf="@+id/tvUserPassword"
                android:layout_toEndOf="@+id/tvUserPassword" />

            <!-- User password TextView -->
            <TextView
                android:id="@+id/tvUserPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mot de passe :"
                android:textSize="18dp"
                android:layout_alignBottom="@+id/etUserPassword"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <!-- Button to get user's touch event -->
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="Continuer"
                android:id="@+id/button_disconnect"
                android:layout_below="@id/etUserPassword"
                android:layout_centerHorizontal="true" />

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >

    <!-- Permissions -->
    ...

    <application
        ... >

        <activity
            android:name="..."
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"
            android:windowSoftInputMode="adjustResize|stateHidden">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在我的MainActivity中,我有DrawerLayout和一些与Toolbar / layout无关的代码行,除了:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    setSupportActionBar(toolbar);

    // enabling action bar app icon and behaving it as toggle button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

    ...
}


// A method to find height of the status bar
// Source : an Stackoverflow's answer
public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

1
你能发布你的代码吗? - adao7000
当然可以!我刚刚更新了我的帖子 :) (另外,我已经解决了我的 FloatingActionButton 问题) - Rascafr
1个回答

1

好的,终于找到了一个解决方案。在 Fragment 中使用这段代码解决所有问题。

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

顺便提一下,使用AppCompatActivity而不是ActionBarActivity(支持V7),我不确定它是否会有所改变,但以防万一...(来自:这个答案https://dev59.com/qGgu5IYBdhLWcg3we3Ct#28012530

我尝试过了,但只能运行一次。关闭键盘并再次打开会出现相同的问题。 - Boy
允许视图在键盘上,但会失去状态栏的透明度。 - Anurag Bhalekar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接