Android:点击按钮后显示图片

时间:2012-03-04 21:15:36

标签: android image button onclick click

我正在寻找这个2天。我无法弄清楚如何显示图像(在我的res文件夹中)如何在点击按钮后显示图像?

2 个答案:

答案 0 :(得分: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和两个布局一个用于主要活动,另一个用于弹出窗口

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);

     }

当用户按下按钮时,此代码将以全屏显示图像,除此之外,请分享您的代码....

答案 1 :(得分:0)

好的,请先注意Android网站上有tutorials和文档(例如ImageView),以及整个网络上的示例程序。

这个answer向您展示了如何组合这样的教程和全屏选项。

通常,图像显示在ImageView中。您调用哪种方法取决于图像的可用方式:如果将其作为静态资源添加到项目中,则可以使用setImageResource(int resId)。如果您已将其作为位图或drawable下载,请使用相应的setX方法。

希望这有帮助。

相关问题