Scrollview不在AbsoluteLayout中滚动

时间:2017-03-23 21:51:25

标签: java android android-studio scrollview

我试图创建一个允许用户按下按钮的Activity,当他按下它时,它会生成一个EditText和一个微调器。问题是滚动视图不滚动。页面保持不变。 XML:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_order_items"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nir.nestleapp.OrderItemsActivity">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true">

        <AbsoluteLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:layout_width="70dp"
                android:layout_height="40dp"
                android:layout_y="58dp"
                android:inputType="phone"
                android:ems="10"
                android:id="@+id/Quantity"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:hint="quantity"
                android:layout_x="11dp" />

            <Spinner
                android:layout_width="174dp"
                android:layout_height="48dp"
                android:layout_x="148dp"
                android:layout_y="58dp"
                android:id="@+id/ItemSpinner" />
        </AbsoluteLayout>
    </ScrollView>

    <Button
        android:text="add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="133dp"
        android:layout_y="431dp"
        android:id="@+id/Generate" />
</AbsoluteLayout>

爪哇:

package com.example.nir.nestleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class OrderItemsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_items);
        final AbsoluteLayout layout = (AbsoluteLayout) findViewById(R.id.activity_order_items);
        final Spinner Items=(Spinner)findViewById(R.id.ItemSpinner);
        final Button Generate=(Button)findViewById(R.id.Generate);
        final EditText Quantity=(EditText)findViewById(R.id.Quantity);
        final float[] yPlacement = {280};
        Generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Spinner GeneratedSpinner=new Spinner(OrderItemsActivity.this);
                EditText GeneratedQuantity=new EditText(OrderItemsActivity.this);
                layout.addView(GeneratedSpinner);
                GeneratedSpinner.setX(Items.getX());
                yPlacement[0] =yPlacement[0]+200;
                GeneratedSpinner.setY(yPlacement[0]);
                layout.addView(GeneratedQuantity);
                GeneratedQuantity.setX(Quantity.getX());
                GeneratedQuantity.setY(yPlacement[0]);
                GeneratedQuantity.setHint("quantity");
                GeneratedQuantity.setWidth(300);
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

您可以更改绝对布局,用作子LinearLayout

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:layout_y="58dp"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/Quantity"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:hint="quantity"
            android:layout_x="11dp" />

        <Spinner
            android:layout_width="174dp"
            android:layout_height="48dp"
            android:layout_x="148dp"
            android:layout_y="58dp"
            android:id="@+id/ItemSpinner" />
    </LinearLayout>
</ScrollView>

答案 1 :(得分:0)

AbsoluteLayout更改为LinearLayout是摆脱此问题的最佳方法。