2012-03-04 242 views
0

我在尋找這個爲期2天。我無法弄清楚如何顯示圖像(在我的res文件夾中)如何在單擊按鈕後顯示圖像?Android:點擊按鈕後顯示圖片

+0

應提供多一點信息:如何d啊,你要顯示它,也許包括不使用的代碼(部分)... – Patrick 2012-03-04 21:25:11

+0

我想點擊一個按鈕,我寫這View.OnClickListener時間表=新View.OnClickListener後在我的res文件夾顯示的圖像() {public void onClick(View v){//我不知道我必須在這裏寫什麼?}我怎麼能更具體? – 2012-03-04 21:49:05

+0

如果你想顯示這將是有益的veery ... ImageView的,在ImageView的一個對話框...;) – Patrick 2012-03-04 21:53:29

回答

7

活動一:

public class TestbuttontestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button btn=(Button)findViewById(R.id.widget45); 
     btn.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
      Intent inf=new Intent(TestbuttontestActivity.this,Activityfullscreen.class); 

      startActivity(inf); 
      } 
      }); 
    } 
} 

活動二:

public class Activityfullscreen extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.calc); 
     ImageView img=(ImageView)findViewById(R.id.widget45); 
     img.setBackgroundResource(R.drawable.digitallovesaktid); 
    } 
} 

的AndroidManifest.xml:

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".TestbuttontestActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 
      android:name="Activityfullscreen"></activity> 
    </application> 

活動一個佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <Button 
    android:id="@+id/widget45" 
     android:layout_width="fill_parent" 
     android:layout_height="120dp" 
     /> 
     <TextView 
    android:id="@+id/widget45" 
     android:layout_width="fill_parent" 
     android:layout_height="120dp" 
     /> 
</LinearLayout> 

活動二佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ImageView 
    android:id="@+id/widget45" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     /> 


</LinearLayout> 

和第二和最佳的方式使用popupwindow和兩個佈局一個主要活動,另一個用於popupwindow

private void showpopup() 
    { 
     LayoutInflater inflater = this.getLayoutInflater(); 
     View mView= inflater.inflate(R.layout.popup,(ViewGroup)findViewById(R.id.lnparentpopup)); 
     PopupWindow mPopupWindow = new PopupWindow(mView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, false); 
     mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); 

     TextView TV=(TextView)this.findViewById(R.id.txtmain);   
     // TableLayout L1 = (TableLayout)findViewById(R.id.tblntarialview); 

     mPopupWindow.showAtLocation(TV, Gravity.CENTER, 45, 0); 

    } 

該代碼會顯示您的圖片在全屏模式下用戶按下按鈕,你從這個要分開請分享您的代碼是什麼....

+0

謝謝,我會試試這個。如果我想要全屏顯示圖像,我必須創建兩個活動。我無法用方法顯示圖像。我是否正確? – 2012-03-04 22:46:48

+0

謝謝,但我想通過使用popupwindow的另一種方法,你可以嘗試它,否則我會嘗試你... – 2012-03-04 22:50:52

0

好吧,請注意先有tutorials和Android網站上的文檔(如ImageView),以及所有網站上的示例程序。

answer告訴您如何這樣的教程和全屏選項結合起來。

一般,圖像被顯示在ImageView的。您調用的方法取決於您的圖像的可用性:如果您將其作爲靜態資源添加到項目中,則可以使用setImageResource(int resId)。如果您已經將它作爲位圖或繪圖下載,請使用相應的setX方法。

希望這會有所幫助。

+0

沒有任何方法像setBackground()來顯示圖像?是觀看創建新班級的圖像的唯一方式? – 2012-03-04 22:22:28

+0

我還沒有嘗試過爲止,但在View類這樣的[方法](http://developer.android.com/reference/android/view/View.html#setBackgroundResource(INT))。但是你應該問問自己,你是否想將某些東西的背景設置爲圖像並將其放在它上面 - 或者只是想顯示簡單而簡單的圖像( - > ImageView) – Patrick 2012-03-04 22:26:32

+0

它顯示我的圖像,但作爲背景,所以我可以看到我的圖像上的其他按鈕。有什麼方法可以顯示完整圖像而不是背景嗎? – 2012-03-04 22:29:36