popwindow1    popwindow2  

 


 

public class MainActivity extends Activity {
Button btn_open;
String msg[] = { "111", "222", "333", "444", "555" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_open = (Button) this.findViewById(R.id.open);
btn_open.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
openPopupWindow(v);
}
});
}

private void openPopupWindow(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pop, null);
// LinearLayout layout = (LinearLayout) popupView
// .findViewById(R.id.);
// layout.setBackgroundColor(Color.RED);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);// 取得焦點
// 點擊空白的地方關閉PopupWindow
ListView lv = (ListView) popupView.findViewById(R.id.listView1);
BaseAdapter adapter = new BaseAdapter() {

@Override
public int getCount() {

return msg.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View v = MainActivity.this.getLayoutInflater().inflate(
R.layout.add, null);
final int index=arg0;
LinearLayout layout = (LinearLayout) v.findViewById(R.id.layout);
ImageView imv= (ImageView) v.findViewById(R.id.imageView1);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.update3);
bitmap=Bitmap.createScaledBitmap(bitmap, 80, 80, false);
imv.setImageBitmap(bitmap);

layout.setBackgroundColor(Color.GRAY);
layout.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
switch(index)
{
case 0:
setTitle("0");
popupWindow.dismiss();
break;
case 1:
setTitle("1");
popupWindow.dismiss();
break;
case 2:
setTitle("2");
popupWindow.dismiss();
break;
case 3:
setTitle("3");
popupWindow.dismiss();
break;

}

}
});
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(msg[arg0]);

return v;
}
};

popupWindow.setBackgroundDrawable(new BitmapDrawable());

popupWindow.showAsDropDown(btn_open);
popupWindow.setFocusable(true);
popupWindow.update();
lv.setAdapter(adapter);
}

}

 


 

pop.xml:

 

 

<?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" >

 

<ListView
android:id="@+id/listView1"
android:layout_width="204dp"
android:layout_height="match_parent" >
</ListView>

 

</LinearLayout>

 


 

 

add.xml:

 

<?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" >

<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/update3" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

</LinearLayout>

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 小Chiu 的頭像
    小Chiu

    Cuiu's Home

    小Chiu 發表在 痞客邦 留言(0) 人氣()