Java ViewSwitcher.ViewFactory makeView()方法的编程方法

Java ViewSwitcher.ViewFactory makeView()方法的编程方法,java,android,Java,Android,我一直在尝试创建一个简单的类来实现ViewSwitcher.ViewFactory接口,该接口基于“Sams自学24小时Android应用程序开发”中开发的项目。在本例中,makeView()方法对布局进行充气以获得视图。然而,我想以编程的方式来完成它,但它不起作用 活动的onCreate()方法如下所示: private TextSwitcher mQuestionText; /** Called when the activity is first created. */ @Overrid

我一直在尝试创建一个简单的类来实现ViewSwitcher.ViewFactory接口,该接口基于“Sams自学24小时Android应用程序开发”中开发的项目。在本例中,makeView()方法对布局进行充气以获得视图。然而,我想以编程的方式来完成它,但它不起作用

活动的onCreate()方法如下所示:

private TextSwitcher mQuestionText;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    mQuestionText = (TextSwitcher) findViewById(R.id.MyTextSwitcher);
    mQuestionText.setFactory(new MyTextSwitcherFactory());
    mQuestionText.setCurrentText("blablabla");
}
private class MyTextSwitcherFactory implements ViewSwitcher.ViewFactory {
    public View makeView() {
        TextView textView = (TextView) LayoutInflater.from(
        getApplicationContext()).inflate(
        R.layout.text_switcher_view,
        mQuestionText, false);
        return textView;
    }
}
private class MyImageSwitcherFactory implements ViewSwitcher.ViewFactory {
    public View makeView() {
        TextView textView = new TextView(getApplicationContext());
        textView.setLayoutParams(new TextSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        textView.setTextColor(R.color.title_color);
        textView.setTextSize(R.dimen.game_question_size);
        textView.setGravity(Gravity.CENTER);
        textView.setText("Test");
            return textView;
    }
}
建议的解决方案如下所示:

private TextSwitcher mQuestionText;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    mQuestionText = (TextSwitcher) findViewById(R.id.MyTextSwitcher);
    mQuestionText.setFactory(new MyTextSwitcherFactory());
    mQuestionText.setCurrentText("blablabla");
}
private class MyTextSwitcherFactory implements ViewSwitcher.ViewFactory {
    public View makeView() {
        TextView textView = (TextView) LayoutInflater.from(
        getApplicationContext()).inflate(
        R.layout.text_switcher_view,
        mQuestionText, false);
        return textView;
    }
}
private class MyImageSwitcherFactory implements ViewSwitcher.ViewFactory {
    public View makeView() {
        TextView textView = new TextView(getApplicationContext());
        textView.setLayoutParams(new TextSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        textView.setTextColor(R.color.title_color);
        textView.setTextSize(R.dimen.game_question_size);
        textView.setGravity(Gravity.CENTER);
        textView.setText("Test");
            return textView;
    }
}
资源文件是:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:textColor="@color/title_color"
    android:textSize="@dimen/game_question_size"
    android:gravity="center"
    android:text="Testing String"
    android:layout_height="match_parent"
    android:padding="10dp">
</TextView>
当我使用充气方法但不以编程方式进行时,会显示“bla”文本。你能指出我代码中的一个错误吗

mQuestionText = (TextSwitcher) findViewById(R.id.MyTextSwitcher);
您正在通过id
MyTextSwitcher
查找视图。如果以编程方式创建视图,请确保设置了该id

view.setId(R.id.MyTextSwitcher);
更新:

哎呀,你的代码读得不够仔细。您是对的,因为您是从XML膨胀的,所以您的ID应该已经正确设置了。您可能缺少的实际上是将视图添加到视图层次结构中。您需要找到一个父项(例如
LinearLayout
或“RelativeLayout
”等),您需要在其下放置
MyTextSwitcher
,让我们称之为
root`并添加如下内容:

root.addView(view);

我发现我提供的代码有什么问题。这是错误的,因为它将资源的ID作为函数参数而不是值本身

textView.setTextColor(R.color.title_color);
textView.setTextSize(R.dimen.game_question_size);
这是正确的:

textView.setTextColor(getApplicationContext().getResources().getColor(R.color.title_color));
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getApplicationContext().getResources().getDimension(R.dimen.game_question_size));

那没用。
mQuestionText
成员使用
R.id.MyTextSwitcher
id正确初始化,如果我理解正确,工厂会提供两个文本视图。我不明白为什么要将
makeView()
方法中提供的TextView的ID设置为TextSwitcher具有的相同ID。我查看了调试器,发现带有“blabla”的TextView是TextSwitcher的子视图,因此初始化似乎工作正常(即使没有
setID()
调用)。但是,它仍然没有显示。更新了一点,你必须做的。