首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:如何从膨胀的自定义组件中访问xml资源

Android:如何从膨胀的自定义组件中访问xml资源
EN

Stack Overflow用户
提问于 2012-03-22 19:53:29
回答 2查看 1.3K关注 0票数 0

我对Android相当陌生,我正试图在我的android应用程序中构建一个自定义组件。为此,我正在扩展FrameLayout,以便我的自定义组件可以由视图顶部的旋转器组成(我最终将在视图中绘制,但目前它只是空白)。

在扩展FrameLayout的类中,我希望设置spinner按钮,这是我用xml列出的。但是,从该类中调用findViewById返回null。在这里读取类似问题的答案后,将返回空。我认为,我需要从一个活动调用findViewById (实际上,如果我从扩展活动的类中设置了自旋器,则一切正常)。然后,一种解决方案是将一个活动传递给我的FrameLayout类的构造函数。但是,我从不亲自调用我的FrameLayout类的构造函数,因为我使用一个布局充气器来膨胀它。我假设布局充气器正在调用我的FrameLayout类的构造函数,我不知道如何向它的构造函数调用中添加一个参数。

有人能告诉我我做错了什么吗,或者我该怎么处理这件事?

下面是我的代码的简化版本:

我的自定义FrameLayout类:

代码语言:javascript
复制
package com.example.myoscilloscope.gui;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Spinner;

import com.example.myoscilloscope.R;

public class Oscilloscope extends FrameLayout {

    //variables
    private Spinner chanSelector; //the channel selector

    // overloading constructors
    public Oscilloscope(Context context) { this(context, null, 0); }
    public Oscilloscope(Context context, AttributeSet attrs) { this(context, attrs, 0); }

    public Oscilloscope(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

            //set up the spinner.
            chanSelector = (Spinner) findViewById(R.id.channel_spinner);

            if(chanSelector == null){
                Log.d("FROMTOM", "CHANNELSELECTOR IS NULL!");
            }

    }

}

我的活动课很大,但是示波器是按下按钮充气的,所以相关的部分(我想)是:

代码语言:javascript
复制
import com.example.myoscilloscope.gui.Oscilloscope;

// ...

public void addchannel(View v){ 
    //this initialization is actually done elsewhere, but I've copied it here
    //  for simplicity's sake
    private LayoutInflater oscilloscopeInflater; 
    LinearLayout myOscilloscopeHolder;

    oscilloscopeInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    myOscilloscopeHolder = (LinearLayout)findViewById(R.id.oscilloscopeHolder); 

    Oscilloscope myOscilliscope = (Oscilloscope)oscilloscopeInflater.inflate(R.layout.oscilloscope, myOscilloscopeHolder, false);       
    myOscilloscopeHolder.addView(trOscilloscopes[trChannelsDisplayed]);
}

下面是我的Oscilloscope.xml文件,它是由我的主程序膨胀的:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<com.example.myoscilloscope.gui.Oscilloscope xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/oscilloscope"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >

    <com.example.myoscilloscope.gui.Oscillator
        android:id="@+id/oscillator"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

       <Spinner 
        android:id="@+id/channel_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
    />


</com.example.myoscilloscope.gui.Oscilloscope>

下面是我的main.xml的相关内容:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_color" >

    <LinearLayout
        android:id="@id/oscilloscopeHolder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/rg_channels"
        android:layout_toLeftOf="@id/HistogramArea"
        android:orientation="vertical"
        android:background="#FFFFFF" >


    </LinearLayout>
</RelativeLayout>

希望这是有意义的。任何见解都将不胜感激。

汤姆

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-22 20:08:31

很抱歉没注意到那是不同的观点..。这应该能解决你的问题。

您不应该在构造函数中使用findViewById,因为此时通货膨胀还没有结束。将findViewById代码移动到一个名为onFinishInflate()的覆盖函数中。

代码语言:javascript
复制
    public class Oscilloscope extends FrameLayout {

        private Spinner chanSelector; //the channel selector

        public Oscilloscope(Context context) { this(context, null, 0); }
        public Oscilloscope(Context context, AttributeSet attrs) { this(context, attrs, 0); }
        public Oscilloscope(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        @Override
        public void onFinishInflate(){
                super.onFinishInflate();        

                chanSelector = (Spinner) findViewById(R.id.channel_spinner);

                if (chanSelector == null) {
                    //Should never get here
                }
        }
    }
票数 4
EN

Stack Overflow用户

发布于 2012-03-22 20:14:04

你可以用.

代码语言:javascript
复制
getChildCount();
getChildAt(index);

然后,您可以比较您正在寻找的子ids。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9829500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档