为什么动态创建的元素将在java(android)中清理?

时间:2013-05-26 11:14:46

标签: java android rotation android-linearlayout manifest

我正在为Android开发一个应用程序用于android.in我的应用程序我通过代码动态创建一些TableRows。当我旋转或转到另一个活动并返回时,我创建的元素将被清除。为什么? 我添加

android:configChanges="keyboardHidden|orientation"

到我的Manifest文件,我的问题没有解决。

我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
 package="com.example.alis"
 android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name=".Login"
    android:label="@string/title_activity_login" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".Mainmenu"
    android:label="@string/title_activity_mainmenu" >
    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".CustomerList"
    android:label="@string/title_activity_customer_list" >
    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".SelectProductActivity"
    android:configChanges="keyboardHidden|orientation|screensize"
    android:label="@string/title_activity_select_product" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
   </application>

我的活动:

公共类SelectProductActivity扩展了Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_select_product, menu);
    return true;
}

public void addRow(View v){
   TableLayout tbl=(TableLayout)findViewById(R.id.tblProduct);
   TableRow row=new TableRow(this);
   TableRow.LayoutParams llp = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
   llp.setMargins(0,10,0,10);
   llp.bottomMargin=20;
   row.setLayoutParams(llp);
   row.setBackgroundColor(Color.parseColor("#eeeeee"));

   //
   TextView tv=new TextView(this);
   tv.setText("txt1");
   tv.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );

   row.addView(tv);
   //
   EditText ed=new EditText(this);
   ed.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );
   row.addView(ed);
   //
   TextView tv2=new TextView(this);
   tv2.setText("txt2");
   tv2.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );
   row.addView(tv2);

   //
   EditText ed2=new EditText(this);
   ed2.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,.15f ) );
   row.addView(ed2);

   //
   TextView tv3=new TextView(this);
   tv3.setText("txt3");
   tv3.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, .40f ) );

   row.addView(tv3);
   tbl.addView(row);
     }

  }

1 个答案:

答案 0 :(得分:0)

从Android 3.2开始,您还需要添加&#34; screenSize&#34;:

android:configChanges="keyboardHidden|orientation|screenSize"

在screenSize中记住上面的S!

来源:http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange