是否可以在 Eclipse 中一次预览所有 Android XML 布局文件?
(我不认为在不同的屏幕分辨率、主题或 API 中预览一个单一的布局!)
我有很多类似的对话框,有时我需要预览所有布局才能找到我需要的。
最佳答案
这是一个独立的 Activity :
- 动态加载所有布局 id 值
- 有一个可以循环遍历所有布局的触摸监听器
- 在简短的 Toast 消息中显示当前布局名称
请求:
如果有人可以建议一种在 View 更新后以编程方式添加布局名称以显示在布局上的方法,那就太好了。
public class MainActivity extends Activity
{
List<Integer> layoutIds = new ArrayList<Integer>();
Map<Integer, String> layoutNameMap = new HashMap<Integer, String>();
int curIdx = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
loadLayoutIds();
showNext();
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
showNext();
return true;
}
private void showNext()
{
if (curIdx >= layoutIds.size())
curIdx = 0;
int layoutId = (int) layoutIds.get(curIdx);
setContentView(layoutId);
String msg = layoutNameMap.get(layoutId);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
curIdx++;
}
private void loadLayoutIds()
{
try
{
Class<?> c = R.layout.class;
Field[] fields = c.getDeclaredFields();
for (Field field : fields)
{
Class<?> t = field.getType();
if(t == int.class)
{
int layoutId = field.getInt(null);
String name = field.getName();
// Because /layout folder can have different layout files,
// use some prefix to indicate which files are at the "activity level"
// This way the following IF controls which layout files make it in
if ( ! name.startsWith("activity_"))
continue;
Log.d("tag", "cur field = " + name + ", cur layoutId = " + layoutId );
layoutIds.add(layoutId);
layoutNameMap.put(layoutId, name);
}
}
}
catch (Exception e)
{}
}
}
(根据需要添加导入、修饰符、异常处理程序等)
关于android - 在 Eclipse 中预览所有 Android XML 布局文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26758960/