嗨,我是 android 新手,我正在尝试为餐厅制作菜单。我的应用程序将在图像按钮中显示项目列表及其拇指大小的图像。列表中有多个项目。当用户单击图像按钮时,会弹出另一个显示图像的窗口。根据单击的按钮,图像应该会弹出。为此,我使用了 switch 语句。也就是说,如果按下按钮一,R.drawable.imag01 应该传递给弹出窗口类。
开胃菜.java
package com.example.thumbnailzoom;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Appetizer extends Activity implements View.OnClickListener {
ImageButton ibAp1;
int img;
PopupWin pop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appetizer);
DisplayComponent();
ibAp1.setOnClickListener(this);
}
private void DisplayComponent() {
ibAp1 = (ImageButton) findViewById(R.id.Ap1);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Ap1:
img = R.drawable.appetimg01;
pop.setPopupImage(img);
pop.displayImage();
break;
}
}
}
开胃菜.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/appetizerTitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Canape Assortments "
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/appetizerPrice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="1.800"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/appetizerDes1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="20"
android:text="6 pieces of mini toasted bread topped with smoked salmon prawn and turkey"
android:textSize="15sp"
android:textStyle="italic" />
<ImageButton
android:id="@+id/Ap1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="80"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/appetimg01" />
</LinearLayout>
</LinearLayout>
PopupWin.java
package com.example.thumbnailzoom;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
public class PopupWin extends Activity {
private ImageView img;
private int imgValue;
private Button bDismiss;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void setPopupImage(int img){
imgValue = img;
}
public void displayImage() {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
img.setImageResource(imgValue);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Button bDismiss = (Button) popupView.findViewById(R.id.dismiss);
bDismiss.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}
弹出.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/dim_back" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dim_back"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:orientation="vertical"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true" />
<Button
android:id="@+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20sp"
android:text="Go Back"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
安卓清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thumbnailzoom"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo" >
<activity
android:name="com.example.thumbnailzoom.Appetizer"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.thumbnailzoom.PopupWin"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
代码一起编写时有效,但我想将图像弹出的代码与项目列表分开。此代码将显示菜单列表,但当我单击缩略图时,应用程序崩溃。我错过了什么请指导。任何形式的帮助都将受到高度赞赏。先感谢您。并对新手草率的代码感到抱歉。