开发者

how can we create a window frame in Android?

开发者 https://www.devze.com 2023-02-11 06:37 出处:网络
I have created two window frame in JAVA using awt and swing package. My Java code is like: import java.awt.*;

I have created two window frame in JAVA using awt and swing package. My Java code is like:

import java.awt.*;
import javax.swing.*;

public class TopLevelWindow {

    private static void createWindow() {
        JFrame frame = new JFrame("Simple GUI");

        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); 
        JLabel textLabel = new JLabel("Hi An开发者_如何学Gos ",SwingConstants.CENTER);
        textLabel.setPreferredSize(new Dimension(300, 100));
        frame.getContentPane().add(textLabel, BorderLayout.CENTER);

        //Display the window. 
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {

        createWindow();
        createWindow();
    }

}

Now on executing my code I am getting two window like frame and switching between these two frames.

The same thing I want it to do in Android means

  1. How to create a window like frame in Android.
  2. At a time two frames are there and we easily switch between frame.

How do I proceed? Any guidance?


There are no frames in Android. Your Activity represents a window and Dialog can be used as pop-up. There's also possibility to make Activity to behave as a Dialog by using a dialog theme. I would suggest to read and understand basic works of Android before proceeding further. If anything you need to take your Swing/AWT experience with grain of salt, it's not directly applicable to the mobile device platform


There is actually bunch of ways to create windows in android.

If you create and launch a new Activity, you are essentially creating a new fullscreen window.

If you want to create a window on top of and inside an existing window (kinda like an overlay) the easiest way to do is using either the Dialog or PopupWindow class.

If you want to have total control in terms of how the window behaves then you can use the WindowManager to create a window (Dialog and PoupWindow classes do this behind the scenes). This is the low level way of creating window but it also gives you the most flexibility. So Something like this:

WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
wm.addView (myView, myWindowLayoutParams);

The tricky part here is creating and configuring the second paramater WindowManager.LayoutParams. This is where you set the flags for your window which determine how your window will behave (modal vs nonmodal, should it receive touch events, what type of window is it, what animation should it use, what type of keyboard should it use, is it fullscreen or not etc).

There are tons of flags so check the documentation http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

0

精彩评论

暂无评论...
验证码 换一张
取 消