How to create GUI in Android instead of using XML?
I don't like to manage XML
and Java
together, can I create same GUI using Java
languag开发者_JAVA百科e?
How can I do that, can you tell me code for simple Button
?
I will appreciate the proper answer.
Yes, you can.
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
final Button button = new Button(this);
button.setText("Press me!");
setContentView(button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
Can I create same GUI using Java language?
Yes you can create GUI in Java
code also as answered by @dtmilano but in general it's not a good practice for Android
applications. Its easy in case of a small application but if you are going to develop an application for End User than your must have to create GUI using XML files. Its also useful when you want to develop application targeted for multiple devices with different-different display size and different-different languages.
The best practice is that try to avoid creating GUI using Java
and instead use XML
as much you can.
I found this article useful maybe It's good for you too Creating an Android User Inteface in java Code
first you need to create an object for your layout like this
RelativeLayout myLayout = new RelativeLayout(this);
then create your for example button like this
Button myButton = new Button(this);
then the Button view needs to be added as a child to the RelativeLayout view which, in turn, is displayed via a call to the setContentView() method of the activity instance
myLayout.addView(myButton);
setContentView(myLayout);
Once launched, the visible result will be a button containing no text appearing in the top left hand corner of the RelativeLayout view.
Definitely you can design your Android UI using java. Here is a little example for create a Button.
Follow these steps
- import a layout package(here i've import android.widget.RelativeLayout)
- import Button package
- Create a layout object
- Create a button object
- Add button to layout
- Set Content View
Here is the code
package com.example.vmbck.app3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create layout
RelativeLayout myLayout = new RelativeLayout(this);
//set background color of the layout to Green
myLayout.setBackgroundColor(Color.GREEN);
//create button
Button myButton = new Button(this);
//set button's background color to red
myButton.setBackgroundColor(Color.RED);
//set button's text to Click Me
myButton.setText("Click Me");
//add button to layout
myLayout.addView(myButton);
//View the content
setContentView(myLayout);
}
}
If you are using Eclipse, you can go to folder res/layout from your project where you will find the file main.xml Right click this file and choose Open with/Android layout editor There you will see a graphical tool that will generate all that is needed to be included in the main.xml file
精彩评论