繁体   English   中英

javacode / Id中的java android space

[英]java android space in javacode/Id

如何在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是我显示的字符串(字符串数组)。

编辑2

我正在使用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)因此它返回上面的位置编号shit获取该编号并在array找到字符串,并尝试将其与R.java进行匹配

如何开始工作

命名ID时不能有空间。

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

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

例如有效的ID

android:id="@+id/bus_schedules"

例如无效的ID

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

但是,当您想使bus schedules可读性时,还有另一种方法。 这是您可以使用如下的句子案例,

android:id="@+id/BusSchedules"

android命名规则未提供在定义ID时允许使用空格的功能。 我建议您使用下划线(_)

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

android:id="@+id/bus_schedules"

暂无
暂无

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

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