在android

时间:2016-05-04 15:22:15

标签: android android-layout layout-inflater

我正在使用布局充气器生成动态文本视图,编辑android中的文本和按钮。一切都很好,但我得到一个额外的按钮,编辑文本和文本视图。我正在动态填充文本视图的值,它也从第二个文本视图开始填充。    这是我的代码。  我的xml代码,

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<LinearLayout
    android:id="@+id/linearlayoutid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

            />

        <EditText
            android:id="@+id/edittextid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayoutid"
        android:layout_centerHorizontal="true"
        android:text="button" />

</LinearLayout>

我的代码用于动态填充值,

   setContentView(R.layout.activity_main);
   LinearLayout lv = (LinearLayout) findViewById(R.id.linearlayoutid);
    for (int i = 0; i < 41; i++) {
         TextView[] myTextViews = new TextView[41];
         EditText[] editTextarray = new EditText[41];
        View v = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        TextView tv= (TextView) v.findViewById(R.id.textviewid);
        EditText editText = (EditText) v.findViewById(R.id.edittextid);
        myTextViews[i] = tv;
        myTextViews[i].setText(d.get(i));
        editTextarray[i] = editText;
        editText.setCursorVisible(true);
        lv.addView(v);
    }

我在布局中获得了额外的textview,edittext和按钮。我必须得到41,但我得到42而第一个是#34;空的&#34;或者没有填充值。任何帮助都会很高兴。 !感谢

1 个答案:

答案 0 :(得分:0)

你必须创建2个xml文件,一个用于你的活动,一个用于你膨胀的视图。

for activity_main.xml

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/scrollview"
     android:layout_width="match_parent"
     android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</ScrollView>

for myview.xml

<LinearLayout
    android:id="@+id/linearlayoutid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

            />

        <EditText
            android:id="@+id/edittextid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayoutid"
        android:layout_centerHorizontal="true"
        android:text="button" />

</LinearLayout>

代码

setContentView(R.layout.activity_main);
LinearLayout lv = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 41; i++) {
         TextView[] myTextViews = new TextView[41];
         EditText[] editTextarray = new EditText[41];
         View v = LayoutInflater.from(this).inflate(R.layout.myview, null);
         TextView tv= (TextView) v.findViewById(R.id.textviewid);
         EditText editText = (EditText) v.findViewById(R.id.edittextid);
         myTextViews[i] = tv;
         myTextViews[i].setText(d.get(i));
         editTextarray[i] = editText;
         editText.setCursorVisible(true);
         lv.addView(v);
}

但我会再说一遍......使用RecyclerView。