android - 从 XML 获取 View

标签 android xml view

在我的应用程序中,我有一个在 Activity 之间保持不变的导航菜单。为了让我的代码更容易修改,我将它放入自己的 xml 中,并一直在为我的每个屏幕导入我的布局。

为了设置按钮,我制作了一个类,我将其传递给我当前的 Activity 。新类可以毫无问题地找到按钮的 View ,但找不到菜单的封装布局,并在我尝试 findViewById() 时返回 null。由于按钮和布局在相同的 XML 中

public static void setup(Activity a){
    myActivity = a;

    //OFFENDING BIT OF CODE
    View myLayout = (View) myActivity.findViewById(R.layout.navigation_bar); 

    Log.d(TAG, "layout: "+myLayout);

    set_btn = (ImageButton) myActivity.findViewById(R.id.a_settings);
    set_btn.setPressed(false);
    set_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TODO:
        }
    });

    inbox_btn = (ImageButton) myActivity.findViewById(R.id.a_offers);
    inbox_btn.setPressed(false);
    inbox_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TODO:
        }
    });

}

主屏幕 XML

<RelativeLayout android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/navigationBar"
    android:layout_alignParentBottom="true">
    <include android:layout_height="wrap_content"
        android:id="@+id/include1"
        layout="@layout/navigation_bar"
        android:layout_width="wrap_content"></include>
</RelativeLayout>

菜单 XML

<RelativeLayout mlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/NavigationLayout">

    <RelativeLayout android:id="@+id/buttonLayout" 
        android:layout_height="75dip" 
        android:layout_width="fill_parent"  
        android:layout_alignParentBottom="true">

        <ImageButton android:id="@+id/a_settings" 
            android:layout_width="100dip"
            android:background="@drawable/settings_button" 
            android:layout_centerHorizontal="true" 
            android:layout_centerVertical="true"
            android:layout_height="fill_parent"
            android:scaleType="fitXY">
        </ImageButton>

        <ImageButton android:id="@+id/a_wallet" 
            android:layout_width="100dip" 
            android:background="@drawable/wallet_button" 
            android:layout_toLeftOf="@+id/a_settings" 
            android:layout_alignBottom="@+id/a_settings"
            android:layout_height="fill_parent"
            android:scaleType="fitXY">
        </ImageButton>

        <ImageButton android:id="@+id/a_offers" 
            android:layout_width="100dip" 
            android:background="@drawable/offers_button" 
            android:layout_toRightOf="@+id/a_settings" 
            android:layout_alignBottom="@+id/a_settings" 
            android:layout_height="fill_parent"
            android:scaleType="fitXY">
        </ImageButton>

    </RelativeLayout>

</RelativeLayout>

最佳答案

即使您的菜单布局确实名为 navigation_bar,您为包含的菜单布局指定了 include1 的 ID -> android:id="@+id/include1"。如果您想在 Activity 中解决它,请尝试使用 findViewById(R.id.include1)(或者只是尝试查找 ImageButtons,它们也是可见的)。

关于android - 从 XML 获取 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6063478/

相关文章:

c# - 如何从多个嵌套级别发生的 XMLDocument 中删除 XMLNode

android - 为 android 平台调试 ionic 应用程序

Android Marketplace,我如何通过电子邮件发送一个可以从设备打开 Marketplace 的链接?

android - 如何确定 Android OpenGL ES 上的最大纹理内存

android - 如何在 View 类中获取 Activity 引用?

javascript - ampersand.js 多 View 切换器

symfony - 在 sonata admin Bundle 的 ListView 中添加新按钮

javascript - 使用 SearchView 按按钮文本进行搜索

java - 将带有 org.w3c.dom.Element 的 java 对象转换为字符串,并在转换回来时出现错误

Java : Parsing xml file using SAX/XPATH