【发布时间】:2019-07-11 12:04:34
【问题描述】:
我的应用程序中有一个活动,我必须在其中为 ImageButtons 充气。起初我认为它运行良好,但注意到整个视图向下移动。我做错了吗?
我尝试更改 XML 模板的一些功能,但没有解决问题或工作。
// Retrieving view's item
LinearLayout linear = findViewById(R.id.linear_layout_existing_picto);
// Retrieving fields from R.drawable
Field[] fields = R.drawable.class.getFields();
// Sorting drawables by name and inserting into the scrollView
for (final Field field : fields) {
try {
if (field.getName().startsWith("dra_")) {
int currentResId = field.getInt(R.drawable.class);
ImageButton imageButtonDrawable = (ImageButton) getLayoutInflater().inflate(R.layout.template_drawable, null);
imageButtonDrawable.setImageResource(currentResId);
imageButtonDrawable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("result", field.getName());
setResult(AddExistingPictoActivity.RESULT_OK, intent);
finish();
}
});
linear.addView(imageButtonDrawable);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
【问题讨论】:
标签: android xml android-layout android-linearlayout layout-inflater