当孩子聚焦时,Scrollview会滚动到原点

问题描述 投票:0回答:2

我有一个View (RelativeLayout),由ScrollView管理。

该视图持有EditText。每当EditText聚焦时,无论是通过点击还是调用requestFocus()ScrollView都会跳回到它的顶部位置。怎么可能让ScrollView保持在实际位置?代码如下:

public class MyView extends RelativeLayout{
public MyView(Context context){
    super(context);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(4 * 600));
    lp.height = (int)(3000);
    lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
    setLayoutParams(lp);
    setBackgroundColor(0xddeeeeee);
    EditText editText = new EditText(getContext());
    editText.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
    editText.setX(200);
    editText.setY(400);
    editText.setBackgroundColor(0xff0000aa);
    addView(editText);
}

和活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final LinearLayout listView = (LinearLayout) findViewById(R.id.project_list);
    ScrollView scrollView = new ScrollView(getApplicationContext());
    MyView view = new MyView(getApplicationContext());
    scrollView.addView(view);
    listView.addView(scrollView);
}
android android-scrollview
2个回答
0
投票

实际上,如果你希望你的整个布局比你应该使用:

SOFT_INPUT_ADJUST_PAN

含义:

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

0
投票
android:focusableInTouchMode="true"
android:focusable="true"

将这些行添加到Scroll View中的Relative Layout

© www.soinside.com 2019 - 2024. All rights reserved.