以弹出音量设置的界面为例,代码如下:
public class MainActivity extends Activity implements View.OnClickListener {
private LinearLayout volset_layout;
private PopupWindow VolsetMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.b1).setOnClickListener(this);
volset_layout = new LinearLayout(this);
volset_layout.setOrientation(LinearLayout.HORIZONTAL);
volset_layout.setPadding(5, 5, 5, 5);
volset_layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
volset_layout.setGravity(Gravity.CENTER);
volset_layout.setBackgroundColor(Color.LTGRAY);
TextView lbl = new TextView(this);
lbl.setText("音量");
lbl.setTextSize(30);
lbl.setGravity(Gravity.CENTER_VERTICAL);
volset_layout.addView(lbl);
final SeekBar seekBar = new SeekBar(this);
seekBar.setLayoutParams(new ViewGroup.LayoutParams(300, ViewGroup.LayoutParams.WRAP_CONTENT));
seekBar.setMax(100);
seekBar.setProgress(50);
volset_layout.addView(seekBar);
VolsetMenu = new PopupWindow(volset_layout, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
VolsetMenu.setBackgroundDrawable(new BitmapDrawable());
VolsetMenu.setOutsideTouchable(true);
}