首页 > 解决方案 > 此处不允许使用元素形状

问题描述

我试图在我用作布局的 XML 中的 LinearLayout 中放置一个形状元素。但是没有显示形状,我不知道如何得到这个。

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal">

            <TextView...>

            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="oval"
                android:layout_marginLeft="5sp"
                android:layout_marginStart="190sp"
                android:layout_marginTop="25sp">
                <solid android:color="#199fff"/>
                <stroke
                    android:width="2dp"
                    android:color="#444444"/>
            </shape>


        </LinearLayout>

当我将鼠标放在“形状”上时,我收到消息“此处不允许元素形状”。

标签: androidandroid-layoutlayout

解决方案


Shape 需要放入它自己的 XML 文件中的drawable文件夹中。然后您可以将该可绘制对象指定为视图的背景:

<View
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:background="@drawable/your_xml_name_here"
    android:layout_marginLeft="5sp"
    android:layout_marginStart="190sp"
    android:layout_marginTop="25sp" />

推荐阅读