android - 无法从自定义 View 中访问 R.id

标签 android

在我的 foo_layout.xml 文件中,我有一个子类 RelativeLayout:


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.android.myapp.FooView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/pegboard_table"
        android:orientation="vertical"
        android:scaleType="fitXY"
        >
        <ImageView   
        android:id="@+id/triangular"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/pegboard_board"
        android:scaleType="fitXY"
    android:gravity="center"
        android:visibility="invisible"
        />
        <Chronometer
        android:id="@+id/timer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/timer_display"
        android:textSize="40sp"
        android:textColor="#000"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:visibility="invisible"/>
         <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/board_table"
        android:visibility="invisible"
        />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:background="@drawable/tab_bar">
        <!-- android:layout_alignLeft="@id/options_tab"-->
        <ImageView 
        android:id="@+id/game_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/game_select" android:paddingLeft="15sp"/>
        <ImageView 
        android:id="@+id/undo_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/game_select"
        android:layout_weight="1"
        android:src="@drawable/undo"
        />
        <ImageView 
        android:id="@+id/settings_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/undo_select"
        android:layout_weight="1"
        android:src="@drawable/settings"
        />   
        <ImageView 
        android:id="@+id/info_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/settings_select"
        android:layout_weight="1"
        android:src="@drawable/info"
        />
    </LinearLayout>
        </com.android.myapp.FooView>
</FrameLayout>

然后我有一个 java 文件 FooView.java 类扩展 相对布局。


package com.android.myapp;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TableLayout;

public class FooView extends RelativeLayout {

    public FooView(Context context) {
        super(context);
        fillTable();
        // TODO Auto-generated constructor stub
    }

    public FooView(Context context, AttributeSet attrs) {
        super(context, attrs);
        fillTable();
        // TODO Auto-generated constructor stub
    }

    public FooView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        fillTable();
        // TODO Auto-generated constructor stub
    }

    public void fillTable() {
        int board = this.getChildCount();
                View boardView = findViewById(R.id.board_table);
        if (board > 0)
            ;
    }

}

board 始终为 0。 boardView 始终为空。

这似乎不是正确的行为。我已经在 FooView 层次结构中的其他 View 中尝试过它,并且 findViewById() 总是返回 null。

对于我的生活,我无法弄清楚我做错了什么。

最佳答案

首先,您的小部件试图引用自身以外的小部件。调用 findViewById() 将获取您的小部件的子项,而不是布局中其他地方的小部件。

其次,您正在尝试从您的构造函数中执行此操作。那不安全。请等到 onFinishInflate()

第三,您可能不想为那些其他小部件硬连接 R.id 值,但允许通过 View 属性或 setter 对这些值进行配置。要访问这些小部件,给定已配置的 ID,您将调用 findViewById(),而不是针对您自己,而是针对您的 Activity ,该 Activity 是从 getContext() 获得的。

关于android - 无法从自定义 View 中访问 R.id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2375678/

相关文章:

android - Canvas - 在 Android 上放大、移动和缩放

android - build.phonegap 插件不受支持 : cordova-plugin-whitelist @ 1

java - 二维空间中的点线碰撞

android - CouchDB 复制到 Android ...适用于 Android 应用程序、Android 浏览器还是?

android - 使用 Intent 捕获图片后应用程序崩溃

java - 同时在 firebase 中添加多个条目

安卓手机内容信息

android - 在 Android 中升级数据库

android - Saripaar 的@ConfirmPassword 不工作

android - 从相机位置获取 LatLngBounds