从XML获取视图

时间:2011-05-19 18:51:07

标签: android xml view

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

要设置按钮,我创建了一个我通过当前活动的课程。新类在查找按钮的视图时没有问题,但无法找到菜单的封装布局,并在尝试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>

2 个答案:

答案 0 :(得分:2)

即使您的菜单布局确实称为navigation_bar,您也可以为自己的菜单布局添加include1 - &gt;的ID android:id="@+id/include1"。如果您想在活动中解决问题,请尝试findViewById(R.id.include1)(或者只是尝试查找ImageButtons,它们也可见)。

答案 1 :(得分:1)

我不是百分之百,但我会说你可能需要从你的myLayout视图中调用findViewById()(也可能有一个转换问题)。

所以改为:

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

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

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

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

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

}