【问题标题】:How can i access my id from another xml file?如何从另一个 xml 文件访问我的 id?
【发布时间】:2019-06-19 01:14:04
【问题描述】:

我正在制作一个带有实现 com.roughike:bottom-bar:2.3.1 的底部栏,我想在底部栏项目上调用 setOnTabSelectListener,但我无法从 xml 文件中找到带有 R.id 的底部栏项目。我的问题是什么?

我的来自 res/layout/activity_main 的 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_bar"
        >
    </FrameLayout>
    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_50dp"
        android:layout_alignParentBottom="true"
        app:bb_tabXmlResource="@xml/tab"
    >
    </com.roughike.bottombar.BottomBar>
</RelativeLayout>

    enter code here

我的来自 res/xml/tab 的 XML

<?xml version="1.0" encoding="utf-8"?>
<tabs>
    <tab   
        id="@+id/home_tab"
        icon="@drawable/home_btn"
        />
    <tab
        id="@+id/search_tab"
        icon="@drawable/search_btn"
        />
    <tab
        id="@+id/new_post_tab"
        icon="@drawable/new_post_btn"
        />
    <tab
        id="@+id/profile_tab"
        icon="@drawable/profile_btn"
        />
</tabs>

我的课程来自 java/com.ms.ma/MainActivity

​​>
 private void init_bottomBar(){

        bottomBar = findViewById(R.id.bottom_bar);

        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {

                switch (tabId)
                {
                    case R.id.home_tab:      <--here i cant find home_tab-->

                        break;
                }
            }
        });
    }

【问题讨论】:

  • 更新到 Android Studio 3.3 后我遇到了同样的问题
  • 自从将 Android Studio 更新到 3.3 版后,我看到了相同的“无法解析符号”错误,但项目可以编译并运行。由于某种原因,AS 无法解析 xml 文件中的 标记。下面的答案都没有帮助。

标签: java xml android-studio


【解决方案1】:

如果此 id 来自另一个 xml 文件,您将无法在 Activity 中找到您的 id

试试这个方法:

View inflatedView = getLayoutInflater().inflate(R.layout.your_layout, null);
BottomBar bottomBar = (BottomBar) inflatedView.findViewById(R.id.bottom_bar);

bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelected(int tabId) {

            switch (tabId)
            {
                case R.id.home_tab:      

                    break;
            }
        }
    });

【讨论】:

    【解决方案2】:

    你提到的图书馆被贬低了

    com.roughike:bottom-bar:2.3.1
    

    链接:https://github.com/roughike/BottomBar

    这样你就无法访问了。

    【讨论】:

    • 在此链接中您可以看到 xml 文件位于 res/xml/bottombar_tabs.xml 中并在 layout/activity_main.xml 中使用并在 MainActivity.java 中调用,但我不能这样做,无法解析符号' home_tab'
    猜你喜欢
    • 2023-04-05
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多