简体   繁体   English

如何从Java文件(Android)创建.xml文件?

[英]How to create .xml file from java file (Android)?

So i have this java file which basically creates a list of menu items that will take me to that specific activity. 所以我有这个Java文件,它基本上创建了一个菜单项列表,这些菜单项将带我进入该特定活动。 I created the list in java but did not create an .xml file when creating the list. 我在Java中创建了列表,但是在创建列表时没有创建.xml文件。 Normally, when creating a new activity, android creates an .xml and java file for both. 通常,在创建新活动时,android会为两者创建一个.xml和java文件。 I only created a java file for it. 我只为此创建了一个Java文件。 Can android auto generate an .xml file for my menu class or do i have to recreate it? Android是否可以为菜单类自动生成.xml文件,还是必须重新创建它?

Code: 码:

Menu.java Menu.java

package com.AthleteProgram.x.athleteprogram;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity{

String classes [] = {"AddAthlete", "Email"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes);
    setListAdapter(adapter);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String listPosition = classes[position];
    try {
        Class myClass = Class.forName("com.AthleteProgram.x.athleteprogram." + listPosition);
        Intent intent = new Intent(Menu.this, myClass);
        startActivity(intent);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

} }

I tried to create a menu.xml but to no avail: 我试图创建一个menu.xml但无济于事:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.AthleteProgram.x.athleteprogram.Menu">

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

</LinearLayout>

What do I do to get a visual .xml file so I can see exactly what I am working with? 我该怎么做才能获得可视的.xml文件,以便可以准确地看到正在使用的文件? Thank you! 谢谢!

You are missing the part that adds the xml to your class 您缺少将xml添加到类中的部分

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.menu);    //This line add the view to your activity

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes);
    setListAdapter(adapter);

}

You can also create your layout separately by doing right click on res -> New -> Android resource file . 您也可以通过右键单击res-> New-> Android资源文件来单独创建布局。 Give a name to it and set its Resource type value to "layout". 给它命名,并将其资源类型值设置为“ layout”。

Then in your Menu.java file add this line setContentView(R.layout.menu); 然后在Menu.java文件中添加以下行: setContentView(R.layout.menu); within onCreate method after super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);之后的onCreate方法中super.onCreate(savedInstanceState); .

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

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