繁体   English   中英

Android:如何使用x和y在屏幕下方的滚动视图中添加元素(并使其滚动)

[英]Android: How do I add an element to scrollview below the screen using x and y (and have it scroll)

我正在尝试使用屏幕下方的X和Y坐标将元素添加到Android屏幕中,但是在加载时屏幕不会滚动。

我的布局代码

<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>

我的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);
}

我最后得到一个按钮,其中一部分不在屏幕上,但不会滚动以显示其余部分。

知道我在做什么错吗?

您必须将内部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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM