用于设计测试/调试的大纲视图

发布于 2024-12-11 08:57:16 字数 961 浏览 2 评论 0原文

当您运行应用程序以查看视图的位置和大小时,是否有一种简单的方法可以用某种颜色的边框来勾画视图的轮廓? 就像您可以使用插件“Web Developer”在 Firefox 中概述网页上的元素一样。

现在我有一些不同颜色的“形状可绘制”xml 文件,我在不同视图上使用它们。但是,仅仅为了打开和关闭轮廓而需要在布局 xml 文件中的视图中添加和删除所有这些可绘制形状,这并不是最佳选择。

我认为可能可行的一种解决方案(但假设有更好的方法)是,是否可以在一个 xml 中添加所有“shape:s”,并且仍然能够“链接”到与布局 xml 分开的每个文件? 在这种情况下,我可以只有两个 xml 文件,一个在“形状”中包含“笔划”,另一个没有(只是空的“shape:s”),只需更改它们之间的名称,即可打开和关闭大纲。

现在,每个形状文件看起来都像这样(例如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>

,然后添加...

android:background="@drawable/test_border_red"

到布局xml文件中的每个有趣的视图。

如果可以在一个 xml 文件中包含所有“形状”,那么代码应该是什么样的?

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".

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.

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.

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"

to each interesting view in the layout xml file.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

°如果伤别离去 2024-12-18 08:57:16

无需代码:

在您用于测试的设备上,转到 settings ->开发人员选项并打开显示布局边界

问题已解决

No code needed:

On the device you are using for testing go to settings -> developer options and turn on Show layout boundaries

Problem solved

叫思念不要吵 2024-12-18 08:57:16

使用活动的根视图尝试此递归方法,

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);
    }
}

您还可以从“设备”选项卡中选择“转储视图层次结构”,它将转储您当前的布局以及有关视图的所有信息

在此处输入图像描述

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

enter image description here

一种可能的解决方案是您可以拥有一些自定义布局,例如您可以扩展LinearLayout,并重写onDraw方法,

在新的自定义布局的onDraw方法中,您可以获取所有子视图的位置并递归地在它们上绘制笔画

One possible solution is that you can have some custom layouts, e.g. you can extend LinearLayout, and override the onDraw method,

In your new custom layouts's onDraw method, you can get the positions of all child views and draw strokes on them recursively

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文