javacode / Id中的java android空间

时间:2013-06-13 02:03:11

标签: java android xml

如何在java中使用空格或空格替换?

XML

<ListView
    android:id="@+id/bus schedules"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

给我错误的代码

findViewById(R.id.bus schedules)

修改

我正在使用。

int panelId = getResources().getIdentifier(Selection.toLowerCase(),
                "id,drawable", getActivity().getPackageName());

因此我的Selection必须与@+/id相同,或者return 0与无效声明一样,我的Selection是我的显示字符串(字符串数组)。

EDIT2

我正在使用TextView

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.menufragment, container, false);
        int i = getArguments().getInt(SELECTION_NUMBER);
        String Selection = getResources().getStringArray(R.array.Selection_array)[i];

        int panelId = getResources().getIdentifier(Selection.toLowerCase(),
                "id", getActivity().getPackageName());

        rootView.findViewById(panelId);


        getActivity().setTitle(drawerSelection);
        return rootView;
    }

SELECTION_NUMBER来自selectItem(int position)所以它会返回上面的位置编号,上面的数字取这个数字并找到array中的字符串并尝试将其与R.java中的内容相匹配

如何开始工作

2 个答案:

答案 0 :(得分:0)

命名ID时无法占用空间。

此处适用的规则与变量命名或文件管理规则相同。您可以使用_(下划线)来命名ID。

您的ID必须以字母开头,后跟数字。

e.g。有效身份证件

android:id="@+id/bus_schedules"

e.g。 ID无效

android:id="@+id/bus schedules"

但是,如果要在bus schedules中提高可读性,还有一种方法。你可以使用下面的句子案例,

android:id="@+id/BusSchedules"

答案 1 :(得分:0)

android命名规则不允许在定义id时允许空格。我建议你使用下划线(_)

android:id="@+id/bus schedules" 

android:id="@+id/bus_schedules"