弹出软键盘时布局无法调整

时间:2019-05-12 09:22:24

标签: android

我有一个FrameLayout,它是ScrollView的子级, 我将图像设置为框架布局背景,并通过设置EditText的x和y坐标在framelayout的随机坐标上绘制动态EditText。问题是当键盘后面的edittext获得焦点时,布局不会向上滚动。我必须手动滚动它,一旦我开始输入布局,就滚动回到原始位置。

如果我不使用FrameLayout,而是采用垂直方向的LinearLayout并动态添加edittext(无需设置坐标),则效果很好。

清单文件中已经设置了“ AsjustPan”。

这是有效的测试布局和Java文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/sv_root"
    android:fillViewport="true"
    android:scrollbars="none"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/fl_canvas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </LinearLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout_canvas = findViewById(R.id.layout_canvas);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ppd);
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, getResources().getDisplayMetrics().widthPixels, 1908, false);
        layout_canvas.setBackground(new BitmapDrawable(getResources(), scaledBitmap));

        for(int i=0; i<50; i++){
            EditText editText = new EditText(this);
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100);
            layoutParams.topMargin = 10;
            editText.setLayoutParams(layoutParams);
            layout_canvas.addView(editText);
        }
    }
}

下面是无效的测试布局和Java文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/sv_root"
    android:fillViewport="true"
    android:scrollbars="none"
    tools:context=".MainActivity">
    <FrameLayout
        android:id="@+id/layout_canvas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>


</ScrollView>

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FrameLayout layout_canvas = findViewById(R.id.layout_canvas);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ppd);

        Bitmap sBitmap = Bitmap.createScaledBitmap(bitmap, getResources().getDisplayMetrics().widthPixels, 1908, false);
        layout_canvas.setBackground(new BitmapDrawable(getResources(), sBitmap));


        int y = 0;
        for(int i=0; i<60; i++){
            EditText editText = new EditText(this);
            editText.setX(20);
            editText.setY(y);

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
            layout_canvas.addView(editText, layoutParams);
            y = y + 100;
        }

    }
}

有人可以帮助我解决此问题。谢谢

0 个答案:

没有答案