Dans mon application, j'ai un menu de navigation qui est constant entre les activités. Pour rendre mon code un peu plus facile à modifier, je l'ai fait dans son propre XML et je l'ai importé mes mises en page pour chacun de mes écrans.Obtenir des vues à partir de XML
Pour configurer les boutons j'ai fait un cours auquel je passe mon activité actuelle. La nouvelle classe n'a aucun problème à trouver les vues pour les boutons, mais ne peut pas trouver la disposition d'encapsulation pour le menu et renvoie null lorsque j'essaye de findViewById()
. Étant donné que les boutons et la mise en page sont dans le même 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 de l'écran principal
<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>
Menu 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>