我有一个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);
}
实际上,如果你希望你的整个布局比你应该使用:
SOFT_INPUT_ADJUST_PAN
含义:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
android:focusableInTouchMode="true"
android:focusable="true"
将这些行添加到Scroll View中的Relative Layout