findViewById似乎没有返回正确的视图类型?

 收藏

Since we don't have to cast anymore, I expected findViewById to return the correct type, but it doesn't seem to do that. I'm obviously making a very simple mistake here, can you point it out?

I have a TextView's ID (since I created it dynamically) and want to change the text size of that item, this snippet works fine:

        TextView tmpView = findViewById(chain.getIngredientNameId());
        tmpView.setTextSize(8);

但这不是:

        findViewById(chain.getIngredientNameId()).setTextSize(8);

So I assume I have to case it to TextView but none of my attempts seems to work (using () or <>), what obvious thing am I missing?

回复
  • UFO 回复

    如果不保存到变量,则应强制转换。

    TextView tmpView = findViewById(chain.getIngredientNameId());          //TextVeiw
    ((TextView) findViewById(chain.getIngredientNameId())).setTextSize(8); // TextVeiw
    findViewById(chain.getIngredientNameId());                             // View