我不知道你是否會認爲這是一種簡單的方法,但我嘗試了以下方法。
定義要在自己的XML來恢復視圖(我試着用一個按鈕,與公開程度,文玩,...)
button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adminUser2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="@string/admin_user" />
然後在你的主佈局使用充氣
定義插入點
<LinearLayout
android:id="@+id/buttonInsertLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>
在您的活動加載視圖到插入點
private Button admin;
@Override
protected void onResume() {
super.onResume();
admin = (Button)View.inflate(UserLoggin.this, R.layout.button, null);
ViewGroup insert = (ViewGroup)findViewById(R.id.buttonInsertLayout);
insert.addView(admin);
// ...
}
稍後在您的reset()方法中,您可以根據xml內容爲視圖的新實例充氣,並用新的實例替換已修改的實例。
public void resetView() {
ViewGroup parent = (ViewGroup)admin.getParent();
int index = parent.indexOfChild(admin);
parent.removeViewAt(index);
// Inflate a fresh new View corresponding to the initial state of the view
Button adminSave = (Button)View.inflate(UserLoggin.this, R.layout.button, null);
parent.addView(adminSave, index);
}
希望它可以幫助...
我的問題是,是否存在一個簡單的方法來恢復視圖到原來的XML狀態,像'View.reset()'。我不問如何或在哪裏我可以編程改變它。 – wvdz