简体   繁体   English

如何在Android中创建GUI而不是使用XML?

[英]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 language? 我不喜欢一起管理XMLJava ,我可以使用Java语言创建相同的GUI吗? How can I do that, can you tell me code for simple Button ? 我怎么能这样做,你能告诉我简单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? 我可以使用Java语言创建相同的GUI吗?

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. 是的,你可以用@dtmilano的答案在Java代码中创建GUI,但一般来说这对Android应用程序来说不是一个好习惯。 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. 在小型应用程序的情况下很容易,但如果您要为最终用户开发应用程序,则必须使用XML文件创建GUI。 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. 最佳实践是尽量避免使用Java创建GUI,而是尽可能多地使用XML

I found this article useful maybe It's good for you too Creating an Android User Inteface in java Code 我发现这篇文章很有用也许对你有好处在java Code中创建一个Android用户界面

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 然后需要将Button视图作为子项添加到RelativeLayout视图,而RelativeLayout视图又通过调用活动实例的setContentView()方法来显示

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. 启动后,可见结果将是一个按钮,其中不包含出现在RelativeLayout视图左上角的文本。

Definitely you can design your Android UI using java. 绝对可以使用java设计Android UI。 Here is a little example for create a Button. 这是创建Button的一个小例子。

Follow these steps 跟着这些步骤

  1. import a layout package(here i've import android.widget.RelativeLayout) 导入布局包(这里我导入android.widget.RelativeLayout)
  2. import Button package 导入Button包
  3. Create a layout object 创建布局对象
  4. Create a button object 创建一个按钮对象
  5. Add button to layout 添加按钮到布局
  6. 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);
    }

}

如果您正在使用Eclipse,则可以从项目中转到文件夹res / layout,在那里您将找到文件main.xml右键单击此文件并选择使用/ Android布局编辑器打开在那里您将看到一个图形工具,它将生成所有这些需要包含在main.xml文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM