2012-03-04 167 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