I have a compound view which consists of two Buttons and one Text view. I want to edit properties of these child views in the Android Studio Layout Editor but I can't. It only shows basic properties but no properties of my custom view.
我有一个复合视图,它由两个按钮和一个文本视图组成。我想在Android Studio布局编辑器中编辑这些子视图的属性,但是我做不到。它只显示基本属性,但不显示自定义视图的属性。
Does the Android Studio Layout Editor only shows limited number of properties set by default? Is it possible to edit properties of my custom view from there, without manually editing the XML files?
Android Studio布局编辑器是否只显示默认设置的有限属性?是否可以从那里编辑自定义视图的属性,而无需手动编辑XML文件?
Thanks in advance!!
提前谢谢! !
3 个解决方案
#1
2
As described in http://developer.android.com/training/custom-views/create-view.html#customattr you have to add a new ressource (res/values/attrs.xml).
正如http://developer.android.com/training/customviews/create -view.html#customattr中描述的那样,您必须添加一个新的ressource (res/values/attrs.xml)。
<resources>
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
Within your View you have to reference this new ressource
在您的视图中,您必须引用这个新的ressource
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
<com.example.customviews.charting.PieChart
custom:showText="true"
custom:labelPosition="left" />
</LinearLayout>
Now you should see the properties in the editor.
现在您应该在编辑器中看到属性。
#2
0
change
改变
xmlns:sidespinner="http://schemas.android.com/tools"
to
来
xmlns:sidespinner="http://schemas.android.com/apk/res-auto"
android studio will show custom attributes of your custom view.
android studio将显示自定义视图的自定义属性。
be sure you defined custom attributes in res/values/attrs.xml
确保您在res/values/attrs.xml中定义了自定义属性。
#3
0
If you customize a View
with custom attributes, you need to use your own namespace to set your custom attributes.
如果使用自定义属性定制视图,则需要使用自己的名称空间来设置自定义属性。
First, set namespace in the root view of your layout like this:
首先,在布局的根视图中设置名称空间,如下所示:
xmlns:sidespinner="http://schemas.android.com/apk/res-auto"
Since custom attribute names are declared in your res/values/attrs.xml
of your current project or library project, namespace sidespinner
will tell aapt
to obtain attributes from your current project or library project.
因为自定义属性名称是在res/values/attrs中声明的。当前项目或库项目的xml,命名空间sidespinner将告诉aapt从当前项目或库项目中获取属性。
Second, set custom attributes in your custom View
like this:
第二,在自定义视图中设置自定义属性如下:
<your-custom-view
sidespinner:boolean-attr="true"
sidespinner:integer-attr="5"
sidespinner:enum-attr="none"//your enum values
sidespinner:dimen-attr="10dp" />
I hope you are clear about this. If you want to know how to customize attributes, refer to @SteffenTimm's answer.
我希望你明白这一点。如果您想知道如何定制属性,请参考@SteffenTimm的答案。
#1
2
As described in http://developer.android.com/training/custom-views/create-view.html#customattr you have to add a new ressource (res/values/attrs.xml).
正如http://developer.android.com/training/customviews/create -view.html#customattr中描述的那样,您必须添加一个新的ressource (res/values/attrs.xml)。
<resources>
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
Within your View you have to reference this new ressource
在您的视图中,您必须引用这个新的ressource
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
<com.example.customviews.charting.PieChart
custom:showText="true"
custom:labelPosition="left" />
</LinearLayout>
Now you should see the properties in the editor.
现在您应该在编辑器中看到属性。
#2
0
change
改变
xmlns:sidespinner="http://schemas.android.com/tools"
to
来
xmlns:sidespinner="http://schemas.android.com/apk/res-auto"
android studio will show custom attributes of your custom view.
android studio将显示自定义视图的自定义属性。
be sure you defined custom attributes in res/values/attrs.xml
确保您在res/values/attrs.xml中定义了自定义属性。
#3
0
If you customize a View
with custom attributes, you need to use your own namespace to set your custom attributes.
如果使用自定义属性定制视图,则需要使用自己的名称空间来设置自定义属性。
First, set namespace in the root view of your layout like this:
首先,在布局的根视图中设置名称空间,如下所示:
xmlns:sidespinner="http://schemas.android.com/apk/res-auto"
Since custom attribute names are declared in your res/values/attrs.xml
of your current project or library project, namespace sidespinner
will tell aapt
to obtain attributes from your current project or library project.
因为自定义属性名称是在res/values/attrs中声明的。当前项目或库项目的xml,命名空间sidespinner将告诉aapt从当前项目或库项目中获取属性。
Second, set custom attributes in your custom View
like this:
第二,在自定义视图中设置自定义属性如下:
<your-custom-view
sidespinner:boolean-attr="true"
sidespinner:integer-attr="5"
sidespinner:enum-attr="none"//your enum values
sidespinner:dimen-attr="10dp" />
I hope you are clear about this. If you want to know how to customize attributes, refer to @SteffenTimm's answer.
我希望你明白这一点。如果您想知道如何定制属性,请参考@SteffenTimm的答案。