简体   繁体   English

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

[英]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.

相关问题 如何在Android屏幕上添加scrollview? - how to add scrollview to screen in android? 在屏幕下方将项目添加到scrollview? - add items to scrollview below screen? 当我添加ScrollView时,Android空白屏幕 - Android Blank screen when I add the ScrollView 如何在下面的代码中放置ScrollView? - How do I place a ScrollView in the code below? Android ScrollView如何在滚动到屏幕顶部时将组件保留在屏幕上? - Android ScrollView how to keep component on screen when it scroll to top of the screen? Android Google Map:如何使用手机屏幕的x和y在Google Map上画一条线? - Android Google Map : how can I draw a line on Google Map with using the x and y of phone's screen? 如何获得屏幕触摸的x,y坐标? - How do i get the x,y coordinate for a screen touch? 如何滚动 Cardview 并在 Android 的 ToolBar 下方附加 TabLayout? - How do i Scroll Cardview and attach TabLayout below ToolBar in Android? 如果在 Android 中占据整个屏幕,如何滚动和查看列表视图下方的元素? - How to scroll and view an element below a listview if it takes up the entire screen in Android? Android:如何在全屏滚动视图中添加页脚? - Android: How do I add a footer to a fullscreen scrollview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM