键盘覆盖屏幕而不是推高布局?

时间:2015-12-10 08:54:35

标签: android android-layout android-listview scrollview android-softkeyboard

我有一个问题,当我点击我的键盘时,我的屏幕内容不会被推高 - 而键盘只是重叠了我屏幕上的所有内容(即覆盖我的ListView)。这是我的布局结构:

<ScrollView>
  <RelativeLayout>
  </RelativeLayout>
</ScrollView>
<ListView>
</ListView>
<LinearLayout>
  <EditText></EditText>
</LinearLayout>

我将RelativeLayout嵌套在ScrollView中,因为我认为这会让我的整个布局都可以滚动,这样就可以推动&#34;所有布局,以便用户在键入他或她的响应时可以查看这些部分。我的AndroidManifest.xml中也有:android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden"这是一张正在发生的事情的图片:

Image of what is happening

这是我的代码。任何帮助将不胜感激!

<?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:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
    xmlns:fresco="http://schemas.android.com/tools"
    android:id="@+id/comments_coordinator_layout">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/comments_appbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/comments_coordinator_layout"
        >

        <RelativeLayout
            android:layout_marginTop="?attr/actionBarSize"
            android:id="@+id/view_post"
            android:layout_width="match_parent"
            android:paddingRight="5dp"
            android:paddingLeft="5dp"
            android:orientation="horizontal"
            android:layout_height="175dp"
            android:background="#e6e6e6">

            <com.facebook.drawee.view.SimpleDraweeView
                android:layout_marginTop="15dp"
                android:id="@+id/poster_picture"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                fresco:placeholderImage="@mipmap/blank_prof_pic"
                fresco:roundedCornerRadius="5dp"
                />

            <TextView
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:layout_toRightOf="@id/poster_picture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:textStyle="bold"
                android:id="@+id/poster_name"/>

            <TextView
                android:layout_alignParentRight="true"
                android:layout_marginTop="15dp"
                android:layout_toRightOf="@id/poster_name"
                android:layout_marginLeft="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:id="@+id/post_date"/>

            <TextView
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/poster_picture"
                android:layout_below="@id/poster_name"
                android:textSize="20sp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/view_status" />

        </RelativeLayout>

    </ScrollView>

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

        <ListView
            android:layout_marginTop="225dp"
            android:id="@+id/lv_comments_feed"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/send_message">
        </ListView>


        <LinearLayout
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/write_comment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:layout_weight="5"
                android:gravity="top|left"
                android:hint="Comment back!"
                android:inputType="textMultiLine"
                android:scrollHorizontally="false" />

            <Button
                android:id="@+id/send_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:text="send"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

    </RelativeLayout>


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

编辑:我尝试为LinearLayout添加父ScrollView。问题现在是: 当我将鼠标悬停在The vertically scrolling ScrollView should not contain another vertically scrolling widget

上时ListView
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    xmlns:fresco="http://schemas.android.com/tools"
    android:id="@+id/comments_coordinator_layout">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/comments_appbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/comments_coordinator_layout"
        >

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

            <RelativeLayout
                android:layout_marginTop="?attr/actionBarSize"
                android:id="@+id/view_post"
                android:layout_width="match_parent"
                android:paddingRight="5dp"
                android:paddingLeft="5dp"
                android:orientation="horizontal"
                android:layout_height="175dp"
                android:background="#e6e6e6">

                <com.facebook.drawee.view.SimpleDraweeView
                    android:layout_marginTop="15dp"
                    android:id="@+id/poster_picture"
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:layout_marginLeft="10dp"
                    fresco:placeholderImage="@mipmap/blank_prof_pic"
                    fresco:roundedCornerRadius="5dp"
                    />

                <TextView
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="15dp"
                    android:layout_toRightOf="@id/poster_picture"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    android:id="@+id/poster_name"/>

                <TextView
                    android:layout_alignParentRight="true"
                    android:layout_marginTop="15dp"
                    android:layout_toRightOf="@id/poster_name"
                    android:layout_marginLeft="5dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:id="@+id/post_date"/>

                <TextView
                    android:layout_marginLeft="5dp"
                    android:layout_toRightOf="@id/poster_picture"
                    android:layout_below="@id/poster_name"
                    android:textSize="20sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/view_status" />

            </RelativeLayout>

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

                <ListView
                    android:layout_marginTop="225dp"
                    android:id="@+id/lv_comments_feed"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/send_message">
                </ListView>


                <LinearLayout
                    android:id="@+id/send_message"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:layout_alignParentBottom="true"
                    android:orientation="horizontal" >

                    <EditText
                        android:id="@+id/write_comment"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|center_horizontal"
                        android:layout_weight="5"
                        android:gravity="top|left"
                        android:hint="Comment back!"
                        android:inputType="textMultiLine"
                        android:scrollHorizontally="false" />

                    <Button
                        android:id="@+id/send_comment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|center_horizontal"
                        android:gravity="center"
                        android:text="send"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
                </LinearLayout>

            </RelativeLayout>

        </LinearLayout>


    </ScrollView>

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

编辑:我认为最好的解决方案可能是创建一个自定义ListView ...,其中包含每个ListView项目中的相同布局。关于如何实现这一点的任何建议或想法?

编辑:试过@ VanillaBoy的回答。我知道生成的图片是因为我有marginTop = 225 dp但是即使我删除它,当我点击我的EditText时,它只显示我的ListView和上面的RelativeLayout是隐 Image

编辑:根据@ Fllo的回答,我仍然可以重现我在开始时使用此布局时出现的错误,所以这就是我在布局中的内容。此外,如果它有助于我的Android Manifest看起来像android:windowSoftInputMode="adjustResize|stateAlwaysHidden">

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    xmlns:fresco="http://schemas.android.com/tools"
    android:id="@+id/comments_coordinator_layout">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/comments_appbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/comments_coordinator_layout"
        android:fillViewport="true"
        >

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

            <RelativeLayout
                android:layout_marginTop="?attr/actionBarSize"
                android:id="@+id/view_post"
                android:layout_width="match_parent"
                android:paddingRight="5dp"
                android:paddingLeft="5dp"
                android:orientation="horizontal"
                android:layout_height="175dp"
                android:background="#e6e6e6">

                <com.facebook.drawee.view.SimpleDraweeView
                    android:layout_marginTop="15dp"
                    android:id="@+id/poster_picture"
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:layout_marginLeft="10dp"
                    fresco:placeholderImage="@mipmap/blank_prof_pic"
                    fresco:roundedCornerRadius="5dp"
                    />

                <TextView
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="15dp"
                    android:layout_toRightOf="@id/poster_picture"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    android:id="@+id/poster_name"/>

                <TextView
                    android:layout_alignParentRight="true"
                    android:layout_marginTop="15dp"
                    android:layout_toRightOf="@id/poster_name"
                    android:layout_marginLeft="5dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:id="@+id/post_date"/>

                <TextView
                    android:layout_marginLeft="5dp"
                    android:layout_toRightOf="@id/poster_picture"
                    android:layout_below="@id/poster_name"
                    android:textSize="20sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/view_status" />

            </RelativeLayout>

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

                <LinearLayout
                    android:orientation="vertical"
                    android:id="@+id/container_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/send_message">
                </LinearLayout>


                <LinearLayout
                    android:id="@+id/send_message"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:layout_alignParentBottom="true"
                    android:orientation="horizontal" >

                    <EditText
                        android:id="@+id/write_comment"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|center_horizontal"
                        android:layout_weight="5"
                        android:gravity="top|left"
                        android:hint="Comment back!"
                        android:inputType="textMultiLine"
                        android:scrollHorizontally="false" />

                    <Button
                        android:id="@+id/send_comment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|center_horizontal"
                        android:gravity="center"
                        android:text="send"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
                </LinearLayout>

            </RelativeLayout>

        </LinearLayout>


    </ScrollView>

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

2 个答案:

答案 0 :(得分:1)

在活动

下的清单中添加此内容
QString CryptoUtils::hexEncode(QString text)
{
    byte *bytearray = (byte *) text.toLatin1().data();
    int length = text.toLatin1().length();

    return hexEncode(bytearray, length);
}

答案 1 :(得分:1)

在Android中应避免使用内置} ListView。实际上,这些布局有自己的滚动检测和手势。解决方案是防止列表上的滚动手势或计算高度并默认扩展它。但这会产生太多的工作,并且不会尊重Android模式和指南。

而不是按ScrollView替换ListView并保留父{4}},正如我的旧答案所指出的,我找到了一个可行的解决方法,其中包含以下步骤:< / p>

  • 创建自己的LinearLayout
  • 覆盖ScrollView,在键盘打开时重绘ListView
  • 获取onSizeChanged()并在上一个方法中检索其高度
  • 最后,将列表移至上一个ListView并计算偏移量
  • 旁边,为标题图片容器设置lastVisiblePosition
  • 您需要在用户打开之前更新子项的高度 键盘,所以你可以更新lastVisiblePosition的值 headerView

它需要这种布局:

  • 布局的活动:

    onClickListener
  • 标题的布局(位于列表上方):

    EditText
嗯,嗯,这就是......所以,好的去:

创建<CoordinatorLayout> <AppBarLayout> ... </AppBarLayout> <RelativeLayout> <CustomListView /> <LinearLayout> ... </LinearLayout> </RelativeLayout> </CoordinatorLayout>

<RelativeLayout>
    <SimpleDraweeView />
    <TextView />
    ...
</RelativeLayout>

覆盖Listview

public class CustomListView extends ListView {
    // last height item variable (updated from Activity)
    public int lastItemHeight = 0;

    public CustomListView(Context context) {
        super(context, null);
    }

    public CustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

onSizeChanged()的全部内容 当用户点击输入时,@Override protected void onSizeChanged(int w, final int h, int oldw, int oldh) { // save last position visible before resize final int lastPosition = super.getLastVisiblePosition(); // call super SizeChanged method super.onSizeChanged(w, h, oldw, oldh); // after resizing, show the last visible item at the bottom of new listview's height, above the edit text // see : http://developer.android.com/reference/android/widget/AbsListView.html#setSelectionFromTop(int, int) super.setSelectionFromTop(lastPosition, (h - lastItemHeight)); } 变量会更新(在列表下方,这要归功于在ListView中将&#34;标题&#34;图片容器设置为lastItemHeight )。

现在,让我们看看headerView

设置自定义ActivityActivity

ListView

创建Adapter并设置// get custom listview element final CustomListView list = (CustomListView) findViewById(R.id.container_list); // example items child ArrayList<String> items = new ArrayList<>(); for (int n=0; n<25; ++n) items.add("Blablabla n."+n); // example adapter ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_list_text_simple, android.R.id.text1, items);

HeaderView

使用Adapter

处理打开的软键盘
// prepare the header content with picture
View header = getLayoutInflater().inflate(R.layout.test_header_image, null);
// set datas to the header elements (example)
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) header.findViewById(R.id.poster_picture);
draweeView.setImageURI(uri);
// add the header to listview
list.addHeaderView(header, null, false);
// set the adapter
list.setAdapter(adapter);

所有代码都是如此!最后一件事(Ow ..在哪里结束?),这里是布局:

EditText.setOnClickListener的主要布局:

// when the user clicks on EditText...
editMessage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // set a new Thread
        list.post(new Runnable() {
            @Override
            public void run() {
                // get last position of child
                int lastPosition = list.getLastVisiblePosition() - 1;
                // if the list can give the view's last child
                if (list.getChildAt(lastPosition) != null) {
                    // update the height of the last child in custom listview
                    list.lastItemHeight = list.getChildAt(lastPosition).getHeight();
                }
            }
        });
    }
});

Activity的头部布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/comments_coordinator_layout"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">

        <com.package.name.CustomListView
            android:id="@+id/container_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/send_message"/>

        <LinearLayout
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <EditText ... />

            <Button ... />
        </LinearLayout>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

噢!我终于完成了。我真的希望你能有正确的行为。我测试了它,似乎工作得很好。
Here's another workaroundListView,但您必须将列表从底部开始。虽然这个解决方案可以让任何孩子在调整大小时保持在列表底部。

希望你能享受它;)