4

I was wondering what would happen if I wrote one code in XML and another in Java.

I had this code in my program:

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class ActivityName extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }
}

While in my Manifest I had this:

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar">
</activity>

My application started with no issues but ran the Java code and not what I had in XML. I assume this is true for all XML edits but wanted to make sure, if I had a Java code that is similar to XML with a bit of differences, would it use my XML format or Java code?

4

2 回答 2

2

它将始终使用 Java 代码,因为它是第二个运行的。但是,如果您是从 XML 膨胀,并且在执行了一些 Java 操作(对布局)之后进行膨胀,那么 XML 会覆盖您编写的代码。

于 2013-05-28T08:45:15.163 回答
1

代码优先于 xml。您总是可以从您编写的代码中过度定义事物。正如@Cornholio 所指出的,如果您从 java 代码中操作基于 xml 的配置,它可以修改您从 java 代码中设置的内容。

然而,Android 框架永远不会覆盖例如您创建的 xml 布局文件。

于 2013-05-28T08:44:40.160 回答