当前位置: 首页 > 知识库问答 >
问题:

Intellij IDEA/Android Studio中带有合并根标签的预览布局

刘建中
2023-03-14

让我们假设我们正在开发基于LinearLayout的复合组件。因此,我们创建类如下所示:

public class SomeView extends LinearLayout {
    public SomeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOrientation(LinearLayout.VERTICAL);
        View.inflate(context, R.layout.somelayout, this);
    }
}

如果我们使用linearlayout作为somelayout.xml的根,我们将有额外的视图级别,因此我们使用合并标记:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some other text"/>
</merge>

(是Android Studio,Intellij IDEA也是一样的,关于Eclipse我不知道)

共有1个答案

田英卓
2023-03-14

有一个新的parentTag tools属性(在Android Studio2.2中添加),您可以使用它为合并标记指定布局类型,这将使布局在布局编辑器预览中正确呈现。

因此使用您的示例:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:parentTag="LinearLayout"
    tools:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some other text"/>
</merge>

注意:必须同时指定android:layout_widthandroid:layout_height才能在编辑器中正确显示布局。

 类似资料:
  • This action splits and joins tag definition, e.g. converts from <tag/> to <tag></tag> and vice versa. Very useful for XML/XSL developers. 这个功能分割合并标签,例如将<tag/> 转换为 <tag></tag> ,或逆操作。对于 XML/XSL 开发很有用。 <

  • 问题内容: 我正在Android Studio中编辑我的fragment_main.xml,但出现此错误: 多个根标签 这里有问题的代码块是: 我在 EditText 和 Button 之前的括号中得到错误 __ 问题答案: 由于在Android中,每个xml文件都必须只有一个根布局。 只需在LinearLayout中添加EditText和Button。正确的代码如下所示

  • 问题内容: 这是我的应用程序级别gradle文件: 编译项目时出现的完整错误是: 错误:任务’:app:transformDexArchiveWithExternalLibsDexMergerForDebug’的执行失败。 java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并dex

  • 我正在尝试实现一个带有动作条支持的activity,在人像模式下显示一个相机预览表面。我正在计算最佳预览尺寸(从zxing代码),但该代码没有考虑动作条尺寸(即高度),返回拉伸图像预览。以前有人遇到过类似的问题吗?有任何教程,关于打开相机预览考虑动作栏高度? 编辑:我的目标是API级别>=8。 > 我添加了样式: true@NULL true 我没有找到任何与Android兼容的名称:Window

  • 我正在尝试在图形布局编辑器中预览一个。由于它没有要预览的片段布局,我只是创建了一个内部带有的xml,以查看项目的外观,并使用其中一个默认项目布局设置预览列表内容,以供尝试。 问题是: 无论发生什么情况,预览中的列表都是空的 预览我选择的列表内容; (我不知道我做错了什么,现在它起作用了) 我想预览自己的自定义列表项视图,而不仅仅是一个布局,因为当为真时,我将只做几件事 任何想法? 编辑:这个片段是