从 XML 获取视图
在我的应用程序中,我有一个在活动之间保持不变的导航菜单。为了使我的代码更容易修改,我将其放入自己的 xml 中,并将其导入到每个屏幕的布局中。
为了设置按钮,我创建了一个类,并传递了当前的 Activity。新类可以毫无问题地找到按钮的视图,但无法找到菜单的封装布局,并且当我尝试 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>
In my app I have a navigation menu that is constant between activities. To make my code a bit easier to modify I made it into its own xml and have been importing it my layouts for each of my screens.
To setup the buttons I made a class which I pass my current Activity. The new class has no problem finding the views for the buttons but cannot find the encapsulating layout for the menu and returns null when I try to findViewById()
. Since the buttons and layout are in the same 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:
}
});
}
Main Screen 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您的菜单布局确实名为
navigation_bar
,您也为包含的菜单布局指定了include1
的 id ->android:id="@+id/include1"
。如果您想在活动中解决它(或者只是尝试查找 ImageButtons,它们也是可见的),请尝试findViewById(R.id.include1)
。Even though your menu layout is indeed called
navigation_bar
, you gave your included menu layout the id ofinclude1
->android:id="@+id/include1"
. TryfindViewById(R.id.include1)
if you want to resolve it in your activity (or just try to find the ImageButtons, they are also visible).我不是 100% 但我会冒险说你可能需要从 myLayout View 中调用 findViewById() (这也可能有转换问题)。
所以这个:
I'm not 100% but I will go out on a limb and say that you probably need to be calling findViewById() from your myLayout View (which may have a casting issue as well).
So this instead: