我试图找到一种方法来区分 @LayoutRes int 和 @IdRes int 之间的区别。 您可以在下面看到一个简单的示例:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
final TextView textView = new TextView(this);
final LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(textView);
final ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getBaseContext(),
linearLayout.getId()/*ERROR*/,
textView.getId(),
days);
ListView listView = new ListView(this);
listView.setAdapter(adapter);
this.setContentView(listView);
}
}
linearLayout.getId()返回 @IdRes ,结果在ArrayAdapter的构造函数上发生了错误。
问题是构造函数ArrayAdapter上的linearLayout.getId()在android studio上突出显示为错误。
以下是ArrayAdapter的构造函数
public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
@NonNull T[] objects)
第二个输入需要 @LayoutRes int 而不是 @IdRes int ,我从linearLayout.getId()获得
我可以从此linearLayout获取 @LayoutRes 吗?
我感谢任何帮助。