java 如何在 Android 中创建 GUI 而不是使用 XML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4801631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create GUI in Android instead of using XML?
提问by Veer
I don't like to manage XML
and Java
together, can I create same GUI using Java
language?
How can I do that, can you tell me code for simple Button
?
I will appreciate the proper answer.
我不喜欢管理XML
和Java
在一起,我可以创建一个使用相同的GUIJava
的语言?我该怎么做,你能告诉我简单的代码Button
吗?我会很感激正确的答案。
回答by Diego Torres Milano
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
}
});
}
}
回答by Vikas Patidar
Can I create same GUI using Java language?
我可以使用 Java 语言创建相同的 GUI 吗?
Yes you can create GUI in Java
code also as answered by @dtmilanobut 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.
是的,您也可以Java
按照@dtmilano 的回答在代码中创建 GUI,但总的来说,这对于Android
应用程序来说不是一个好习惯。在小型应用程序的情况下很容易,但如果您要为最终用户开发应用程序,则必须使用 XML 文件创建 GUI。当您想要开发针对具有不同不同显示尺寸和不同语言的多个设备的应用程序时,它也很有用。
The best practice is that try to avoid creating GUI using Java
and instead use XML
as much you can.
最佳实践是尽量避免使用创建 GUI Java
,而是XML
尽可能多地使用。
回答by Meysam Hadigheh
I found this article useful maybe It's good for you too Creating an Android User Inteface in java Code
我发现这篇文章很有用,也许它对你也有好处 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
然后需要将 Button 视图作为子视图添加到 RelativeLayout 视图中,然后通过调用 Activity 实例的 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.
一旦启动,可见结果将是一个不包含任何文本的按钮,该按钮出现在相对布局视图的左上角。
回答by hackerbuddy
Definitely you can design your Android UI using java. Here is a little example for create a Button.
当然,您可以使用 java 设计您的 Android UI。这是创建按钮的一个小例子。
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
- 导入一个布局包(这里我导入了 android.widget.RelativeLayout)
- 导入按钮包
- 创建布局对象
- 创建按钮对象
- 将按钮添加到布局
- 设置内容视图
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);
}
}
回答by Antonio Leite
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
如果您使用的是 Eclipse,您可以从您的项目转到文件夹 res/layout,您将在其中找到文件 main.xml 右键单击此文件并选择 Open with/Android layout editor 在那里您将看到一个图形工具,它将生成所有这些需要包含在 main.xml 文件中