2017-03-22 84 views
-1

我有3個水平線性佈局,我在其中以編程方式添加圖像查看。當我關閉應用程序並重新打開它時,它不會保存我添加的圖像視圖。我該如何保存這些圖像並將其重新加載到簡歷上?以編程方式保存圖像查看

XML:

<TextView 
    android:id="@+id/textView8" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Chickens" 
    android:textColor="#000000" 
    android:textSize="24sp" 
    tools:ignore="HardcodedText" /> 

<LinearLayout 
    android:id="@+id/Chickens" 
    android:layout_width="match_parent" 
    android:layout_height="65dp" 
    android:orientation="horizontal"> 

</LinearLayout> 

<TextView 
    android:id="@+id/textView9" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Cows" 
    android:textColor="#000000" 
    android:textSize="24dp" 
    tools:ignore="HardcodedText,SpUsage" /> 

<LinearLayout 
    android:id="@+id/Cows" 
    android:layout_width="match_parent" 
    android:layout_height="65dp" 
    android:orientation="horizontal"> 

</LinearLayout> 

<TextView 
    android:id="@+id/textView10" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Horses" 
    android:textColor="#000000" 
    android:textSize="24sp" 
    tools:ignore="HardcodedText" /> 

<LinearLayout 
    android:id="@+id/Horses" 
    android:layout_width="match_parent" 
    android:layout_height="65dp" 
    android:orientation="horizontal"> 

</LinearLayout> 

在活動時間:

Chickens = (LinearLayout) findViewById(R.id.Chickens); 
Cows = (LinearLayout) findViewById(R.id.Cows); 
Horses = (LinearLayout) findViewById(R.id.Horses); 

ImageView houseForChickens = new ImageView(this); 
houseForChickens.setImageResource(R.drawable.house); 
Chickens.addView(houseForChickens, 300, 300); 
+0

保存imageview?你的意思是src?你是如何從webservice獲得圖像的?或本地。如果來自webservice,則可以將鏈接保存在數據庫或共享首選項中。如果你從drawerable(本地)獲得它,你不需要保存,你可以重新加載它。 – kggoh

+0

imageview是從drawable添加的:ImageView houseForChickens = new ImageView(this); houseForChickens.setImageResource(R.drawable.house);)然後我將它添加到佈局中(Chickens.addView(houseForChickens,300,300);),我該如何重新加載它?我是一個初學者.. – Anr14

+0

因爲你說你關閉了應用程序並重新打開它。所以應用程序重新啓動,它將再次運行imageView的加載。如果你的應用使用片段。片段加載時,它也會加載圖像。也許你向我展示你的圖像所在的.xml文件。以及你如何調用這個XML。 – kggoh

回答

0

添加在您的XLM

<ImageView 
      android:id="@+id/iv_houseForChickens " 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:src="@drawable/R.drawable.house"/> 

在代碼

ImageView houseForChickens = (ImageView) findViewById(R.id.iv_houseForChickens ); 

看看你是否想在代碼中設置src或者放到xlm中,由你決定

+0

它沒有工作,但無論如何謝謝你!我在佈局中用一個按鈕添加新的「房屋」,如果我按照你告訴我的方式現在按下它,它就不起作用。也許這是我的錯,我會看看我能否修復它。快樂編碼也:) – Anr14