如何将很多textview更改为滚动列表布局

时间:2014-12-01 00:23:14

标签: android android-layout listview layout android-listview

我是编程的开始,我需要你的帮助。我必须编写应用程序,它将显示传感器结果作为文本,我做错了方法(代码工作,但屏幕不滚动)。

下面我复制XML文件:

android:layout_width="wrap_content"
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=".MainActivity" >

<TextView
    android:id="@+id/rLight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" 
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/rPressure"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rLight"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/rHumidity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rPressure"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/rProximity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rHumidity"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/rTemp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rProximity"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rAccel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rTemp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rGyro"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rAccel"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rMag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rGyro"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rRotation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rMag"/>

 <TextView
    android:id="@+id/rGravity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rRotation"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/rSCounter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rGravity"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rSDetector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rSCounter"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rLinearAcc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rSDetector"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rSignMotion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rLinearAcc"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/rNoise"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rSignMotion"
    android:textAppearance="?android:attr/textAppearanceMedium" />

这是主要课程:

公共类MainActivity extends Activity实现了EventListener,StepCounterListener,LinearAccListener,StepDetectorListener,LightSensorListener,PressureSensorListener,GravitySensorListener,RotationVectorListener,RelativeHumidityListener,ProximitySensorListener,TempSensorListener,AccelerometerSensorListener,GyroSensorListener,MagSensorListener {

private TextView lightText, pressureText, signText, noiseText, humidityText, linearAccText, proximityText, tempText, accelText, gyroText, magText, rotationText, gravityText, stepDetectorText, stepCounterText;
private LightSensor mLightSensor;
private PressureSensor mPressureSensor;
private RelativeHumiditySensor mRelativeHumidity;
private ProximitySensor mProximitySensor;
private TemperatureSensor mTempSensor;
private AccelerometerSensor mAccelSensor;    
private GyroscopeSensor mGyroSensor;
private MagnetometerSensor mMagSensor;
private RotationSensor mRotSensor;
private GravitySensor mGravitySensor;
private StepCounterSensor mStepCounter;
private StepDetectorSensor mStepDetector;
private LinearAccelerometer mLinearAcc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);

    lightText = (TextView)findViewById(R.id.rLight);
    pressureText = (TextView)findViewById(R.id.rPressure);
    humidityText = (TextView)findViewById(R.id.rHumidity);
    proximityText = (TextView)findViewById(R.id.rProximity);
    tempText = (TextView)findViewById(R.id.rTemp);
    accelText = (TextView)findViewById(R.id.rAccel);
    gyroText = (TextView)findViewById(R.id.rGyro);
    magText = (TextView)findViewById(R.id.rMag);
    rotationText = (TextView)findViewById(R.id.rRotation);
    gravityText = (TextView)findViewById(R.id.rGravity);
    stepCounterText = (TextView)findViewById(R.id.rSCounter);
    stepDetectorText = (TextView)findViewById(R.id.rSDetector);
    linearAccText = (TextView)findViewById(R.id.rLinearAcc);
    signText = (TextView)findViewById(R.id.rSignMotion);
    noiseText = (TextView)findViewById(R.id.rNoise);


    mLightSensor = new LightSensor(getBaseContext());
    mLightSensor.setListener(this);

    mPressureSensor = new PressureSensor(getBaseContext());
    mPressureSensor.setListener(this);

    mRelativeHumidity = new RelativeHumiditySensor(getBaseContext());
    mRelativeHumidity.setListener(this);

    mProximitySensor = new ProximitySensor(getBaseContext());
    mProximitySensor.setListener(this);

    mTempSensor = new TemperatureSensor(getBaseContext());
    mTempSensor.setListener((TempSensorListener) this);
    [...]

}


public void onLightSensorChanged(String lux){
    lightText.setText("LIGHT level: " + lux);
}

public void onPressureSensorChanged(String pressure){
    //items.add("PRESSURE: " + pressure);
    pressureText.setText("PRESSURE: " + pressure);
}

[...]

我尝试使用ArrayString,listAdapter,但我总是遇到很多错误..请帮忙

2 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
        <TextView
            android:id="@+id/rLight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/rPressure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        .....................

    </LinearLayout>
</ScrollView>

答案 1 :(得分:0)

你需要使用一个scrollview,只需在它之前插入你的XML内容,它应该工作:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


    </ScrollView>

</LinearLayout>