Android - Create app with pure Java and no XML?

前端 未结 6 912
清酒与你
清酒与你 2020-12-08 14:38

I\'m wondering if it is possible to create an Android app with only Java. No XML, no other things.

In Eclipse, when I create a new Android Project, the Manifest xml-

相关标签:
6条回答
  • 2020-12-08 15:05

    Take a look at this video, just posted by the android team: http://www.parleys.com/#st=5&id=2191&sl=8

    It's all about layouts and includes how to layout apps using Java, not XML. However, you are warned that the android team wants you to use XML...

    0 讨论(0)
  • 2020-12-08 15:09

    Is it possible to avoid XML? Yes, with the exception of the manifest and perhaps some theme declarations (I'm not sure if there are public Java equivalents for everything we can set up via themes).

    Is it a good idea? Heavens, no.

    The point behind the resource system is to allow Android to transparently hand you the proper resources needed by the device at the present moment, based on both permanent device characteristics (e.g., screen density) and transient device characteristics (e.g., portrait vs. landscape orientation).

    To avoid the resources, you will have to go through a bunch of if statements to determine which hunk of Java code to run, detecting all these things by hand. This gets significantly more complicated once you take into account changes in Android itself, as new configuration changes and values get added, making it difficult for you to support everything you need to in a backwards-compatible way.

    Along the way, you will lose all tool support (drag-and-drop GUI building, MOTODEV Studio's string resource assistants, etc.), outside of plain Java editing and debugging.

    You seem to be placing your own personal technical inclinations ahead of all other considerations. If this is some small personal project, that may be a fine attitude. If you are creating code to be developed and/or maintained by others over time, though, you need to factor in the needs of those other developers, and they may be much more open to XML than are you.

    0 讨论(0)
  • 2020-12-08 15:10

    For the layouts you have two options

    1. Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

    2. Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

    So for the first question - yes - you can delete xml layout files (if you must).

    I think you cannot get rid of the manifest.xml..Quoting:

    Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory.

    0 讨论(0)
  • 2020-12-08 15:21

    It is possible to create all layout files from code. But it's reccomended to use the layout XML files.

    The AndroidManifest.xml can not be replaced by code, since the system relies on the data included in this file.

    0 讨论(0)
  • 2020-12-08 15:22

    yes it's possible to make an app with java and no xml.
    here's a simple example.
    i use eclipse.
    first i made my own my class called MyView, which extends View.

    public class MyView extends View {
    
        Paint paint;
    
        public MyView(Context context) {
            super(context);
            setBackgroundColor(Color.BLACK);
            paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setTextSize(50);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawCircle(50, 50, 50, paint);
            canvas.drawText("hello", 0, 150, paint);
            canvas.drawLine(0, 150, 100, 170, paint);
        }
    
    }
    

    in the activity, i create a MyView object called myView.
    i get rid of setContentView(R.layout.activity_main);
    then i type setContentView(myView);

    public class MainActivity extends Activity {
    
        MyView myView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    //      setContentView(R.layout.activity_main); // i delete this
            myView = new MyView(this);
            setContentView(myView);
        }
    }
    

    res\layout\activity_main.xml file can now be deleted.
    res\layout folder can now be deleted.

    0 讨论(0)
  • 2020-12-08 15:31

    I fully agree Van Coding. If you have the proper programming language, you can write much more concise layout definitions than with AXML. A C# example how to replace AXML layouts

    ViewGroup Layout(ViewGroup layout, IList<View> contents) {
        if (contents != null)
            foreach (View v in contents)
                layout.AddView(v);
    
        return layout;
    }
    
    0 讨论(0)
提交回复
热议问题