【问题标题】:How to scroll my layout when setting android:windowSoftInputMode="adjustPan"?设置 android:windowSoftInputMode="adjustPan" 时如何滚动我的布局?
【发布时间】:2012-04-20 04:12:32
【问题描述】:

我的活动有一个顶栏和一个底栏。顶栏和底栏之间的空间我有一个线性布局,里面有几个编辑文本视图。因为我不希望每次出现软键盘时都调整布局大小,所以我在清单中为我的活动设置了 android:windowSoftInputMode="adjustPan" 。但是当软键盘打开时,我想向下滚动以选择另一个要输入的编辑文本,它不允许我这样做。当我关闭软键盘时,我只能选择底部的编辑文本。这是非常烦人和不方便的。 我怎样才能让软键盘的滚动视图和 ajustpan 模式很好地协同工作?

请帮帮我。非常感谢。

【问题讨论】:

  • 我也有同样的问题,但我不喜欢做你为“伪造”所做的编码......嗯
  • 到目前为止,这个解决方案运行良好,我也在寻找更简单的替代方案。希望其他人会针对此问题分享更好的解决方法。
  • 但是当显示软键盘时,此解决方案会隐藏按钮。但是根据屏幕分辨率等,按钮可能不需要隐藏,对吧?
  • 当软键盘出现时,此解决方案不会调整您的视图大小,您可以滚动视图以查看软键盘隐藏的控件。很抱歉我不太了解你的情况。

标签: android android-layout


【解决方案1】:

最后,我找到了解决我的问题的方法,所以我想分享给将来可能遇到同样问题的人。我的布局的简要说明如下:

<myRelativeLayout>
<topbar.../>
<myscrollView>
    <linearLayout>
        //all stuff controls:editview,textview,....
    </linearLayout>
</myscrollView>
<bottombar.../>

我创建自定义类 myRelativeLayout 扩展 RelativeLayout

public class myRelativeLayout extends RelativeLayout{

public interface OnRelativeLayoutChangeListener {
    void onLayoutPushUp();
    void onLayoutPushDown();
}

private OnRelativeLayoutChangeListener layoutChangeListener;
public myRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    final int actualHeight = getHeight();

    if (actualHeight > proposedheight){
        // Keyboard is shown
        layoutChangeListener.onLayoutPushUp();
    } else if(actualHeight < proposedheight){
        // Keyboard is hidden
        layoutChangeListener.onLayoutPushDown();
    }       
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void setLayoutChangeListener(OnRelativeLayoutChangeListener layoutChangeListener) {
    this.layoutChangeListener = layoutChangeListener;
}

public OnRelativeLayoutChangeListener getLayoutChangeListener() {
    return layoutChangeListener;
}

}

在我的活动中,我只是将 myRelativeLayout 的 setLayoutChangeListener 设置为在软键盘显示时隐藏底栏,并在软键盘隐藏时显示底栏:

myRlayout.setLayoutChangeListener(new OnRelativeLayoutChangeListener() {

        @Override
        public void onLayoutPushUp() {
            // TODO Auto-generated method stub
            myBottombar.setVisibility(View.GONE);//in my case i need to setVisibility(View.GONE) to bottombar in order for this bar is not displayed when softkeyboard show up.
        }

        @Override
        public void onLayoutPushDown() {
            // TODO Auto-generated method stub
            myBottombar.setVisibility(View.VISIBLE);// redisplay myBottombar when keyboard is closed.

        }
    });

不要忘记为活动设置 android:windowSoftInputMode="adjustResize"。 希望这对遇到同样问题的人有用。

【讨论】:

  • 谢谢你,但是,没有额外的编码来“伪造”我们想要的功能吗?
【解决方案2】:

像这样将EditText 放在ScrollView 中:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

【讨论】:

  • 我不是问如何在 xml 中进行布局,我想知道当我为我的活动设置 android:windowSoftInputMode="adjustPan" 时如何滚动我的布局。
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
相关资源
最近更新 更多