【问题标题】:Getting Views from XMLs从 XML 中获取视图
【发布时间】:2011-05-19 18:51:06
【问题描述】:

在我的应用程序中,我有一个在活动之间保持不变的导航菜单。为了让我的代码更容易修改,我将它变成了自己的 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>

【问题讨论】:

    标签: android xml view


    【解决方案1】:

    尽管您的菜单布局确实被称为navigation_bar,但您为包含的菜单布局提供了include1 -> android:id="@+id/include1" 的ID。如果您想在活动中解决它,请尝试findViewById(R.id.include1)(或者只是尝试找到 ImageButtons,它们也是可见的)。

    【讨论】:

      【解决方案2】:

      我不是 100%,但我会说你可能需要从你的 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:
              }
          });
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多