[英]Android: How do I add an element to scrollview below the screen using x and y (and have it scroll)
I'm trying to add an element to a screen in Android using X and Y co-ordinates that's beneath the screen but when it loads the screen won't scroll. 我正在尝试使用屏幕下方的X和Y坐标将元素添加到Android屏幕中,但是在加载时屏幕不会滚动。
My code for the layout 我的布局代码
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical"
android:id="@+id/ScrollView"
android:background="#16A901"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/inner"
android:background="#b200C4"
>
</RelativeLayout>
</ScrollView>
My Java code 我的Java代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xycords);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int maxX = size.x;
int maxY = size.y;
System.out.println("bar maxX "+maxX+" maxY "+maxY);
ScrollView sv= (ScrollView)findViewById(R.id.ScrollView);
RelativeLayout inner = (RelativeLayout)findViewById(R.id.inner);
Button button = new Button(this);
button.setBackgroundResource(R.drawable.black_rect_border_yellow);
button.setText("Test 1 2 3");
button.setX(10);
button.setY(661);//652 is the maxY in my set up so this will obscure part of it
LayoutParams wrap= new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
wrap.setMargins(0, 0, 0, 0);
//wrap.addRule(RelativeLayout.ALIGN_LEFT,R.id.vLine);
//wrap.addRule(RelativeLayout.ALIGN_TOP,R.id.hLine);
inner.addView(button, wrap);
}
I end up with a button where part of it is offscreen but it won't scroll to show the rest. 我最后得到一个按钮,其中一部分不在屏幕上,但不会滚动以显示其余部分。
Any idea what I'm doing wrong? 知道我在做什么错吗?
You must set inner layout_height
to a value greater than button y
value: 您必须将内部layout_height
设置为大于y
值的值:
...
inner.addView(button, wrap);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)inner.getLayoutParams();
params.height = 661 + 48;
inner.setLayoutParams(params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.