大纲视图设计测试/调试视图、大纲、测试

2023-09-06 18:04:03 作者:这世间本就没有温柔,而你是温柔本身。今日小编为大家送上淡雅清

有没有一种简单的方法,当你运行一个应用程序看到的位置和大小是如何的看法勾勒在某些颜色的边框的看法?就像你可以在在Firefox网页与插件Web开发人员。

Is there a simple way to outline views with a border in some color when you run an app to see how the position and size is for the views? Like you could outline elements on a webpage in firefox with the addon "web developer".

现在我有一些形绘为我的不同观点用不同的颜色的XML文件。但它不是最佳的需要添加和删除所有这些形状绘制在布局XML文件的意见,只是把大纲和关闭。

Right now I have some "shape drawable" xml files for different colors that I use on different views. But its not optimal to need to add and remove all these shape drawable to the views in the layout xml files just to turn the outline on and off.

的一个解决方案我虽然的,也许可以工作(但假设有更好的方法)是,如果是可以添加所有的形状:的在一个单一的xml和仍然能够链接从每个单独布局XML文件?在这种情况下,我可以有两个XML文件,一个与形的中风,一个没有(只是一句形:S),只是改变他们的名字,打开概述和关闭。

One solution I though of that maybe could work (but suppose there is better ways) is, if it is possible to add all "shape:s" in one single xml and still be able to "link" to each separate from the layout xml files? In that case I could just have two xml files, one with the "stroke" in the "shape", and one without (just empty "shape:s") and just change the name between them, to turn outlining on and off.

目前各种形状文件看起来像这样(前绘制/ test_border_red.xml)

Right now every shape file looks like this (ex drawable/test_border_red.xml)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <stroke 
    android:width="1dp" 
    android:color="#ff0000" 
    />
  <padding 
    android:left="1dp" 
    android:top="1dp" 
    android:right="1dp"
    android:bottom="1dp" 
    />   
  </shape>

,然后加...

and then adding...

android:background="@drawable/test_border_red"

要在布局XML文件中的每个有趣的观点。

to each interesting view in the layout xml file.

如果它可能有所有形在一个XML文件中,应该如何code样子?

If its possible to have all "shape" in one xml file, how should the code look like?

推荐答案

使用活动的根视图试试这个递归方法

Try this recursive method using the root view of the activity

private void outlineViews(View parent){
    if (!(parent instanceof ViewGroup)){
        return;
    }
    ViewGroup vg = (ViewGroup) parent;
    for (int i=0; i<vg.getChildCount(); i++){
        View view = vg.getChildAt(i);
        GradientDrawable gd = new GradientDrawable();
        gd.setStroke(3, Color.RED);
        view.setBackgroundDrawable(gd);
        outlineViews(view);
    }
}

您也可以选择转储观层次,从设备选项卡,将所有有关视图的信息转储你当前的布局

you may also select "Dump view Hierarchy" from the Devices tab, it will dump your current layout with all the information about the view