-1

我想添加到我的应用程序 ScreenView,但我只是收到错误。

我的布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    android:background="@color/bg"
    android:id="@+id/mainlayout"
    tools:context=".NwActivity" android:baselineAligned="false">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/rlA" >

            <TextView 
                android:id="@+id/teamA"
                android:text="@string/teamA"
                android:textSize="24sp"
                style="@style/Txt" />

            <Button
                android:id="@+id/pAddA"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/teamA"
                style="@style/Txt"
                android:text="@string/pAdd"
                android:onClick="addA" />

            <EditText
                android:id="@+id/eta0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/pAddA"
                style="@style/Txt"
                android:hint="@string/pHint" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/rlB" >

            <TextView 
                android:id="@+id/teamB"
                android:text="@string/teamB"
                android:textSize="24sp"
                style="@style/Txt" />

            <Button
                android:id="@+id/pAddB"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/teamB"
                style="@style/Txt"
                android:text="@string/pAdd"
                android:onClick="addB" />

        </RelativeLayout>

    </ScrollView>

</LinearLayout>

活动:

package some.program;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class NwActivity extends Activity {

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

    public int skid = 0;

    public void addA(View view) {
        if(skid < 14) {
            skid = skid + 1;
            //LinearLayout ll = (LinearLayout) findViewById(R.id.mainlayout);
            RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlA);

            EditText et1 = new EditText(this);
            et1.setHint(R.string.pHint);
            String strID = "eta" + skid;
            int nid = getResources().getIdentifier(strID, "id", this.getPackageName());
            et1.setId(nid);

            int oskid = skid - 1;

            int oid;


            String resID = "eta" + oskid;
            oid = getResources().getIdentifier(resID, "id", this.getPackageName());


            RelativeLayout.LayoutParams params1= new RelativeLayout.LayoutParams
                        ( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            params1.addRule(RelativeLayout.BELOW, oid);
            params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
            et1.setLayoutParams(params1);
            et1.setTextAppearance(this, R.style.Txt);
            rl.addView(et1);
        }
    }

    public void addB(View view) {

    }

}

如何添加 ScrollView,不会弄乱我的布局?单击按钮时,应用程序正在创建 EditText 表单。一切正常,但所有 EditText 字段都不适合布局并且不会出现。

4

1 回答 1

0

您的布局中可能还有其他一些问题,我发现的一些问题是

首先ScrollView只需要一个直接的孩子,而你在声明两个视图 rlA 和 rlB ScrollViewScrollView(您可以通过将其作为主要布局并LinearLayout代替来使其工作ScrollView

其次,您应该为布局添加layout_widthlayout_height属性(我猜您已经在STYLES中声明了)。

于 2013-05-30T09:21:37.903 回答