Android 布局编辑器不会将可设置样式的属性传递给自定义视图类
我正在创建一个Android 布局编辑器不会将可设置样式的属性传递给自定义视图类,android,graphical-layout-editor,declare-styleable,Android,Graphical Layout Editor,Declare Styleable,我正在创建一个fixedspectimageview来解决Android中的问题,即设置ImageView的视图边界以匹配其中包含的图像的纵横比,并填充任意宽(或高)的可用区域并适当拉伸图像。在当前版本中,我从布局中发送一个浮点“比率”参数,尽管最终我打算检测图像的比率 但是,我在可设置样式的属性方面遇到了问题。My attrs.xml具有以下特性: <resources> <!-- other stuff, which bizarrely works -->
fixedspectimageview
来解决Android中的问题,即设置ImageView
的视图边界以匹配其中包含的图像的纵横比,并填充任意宽(或高)的可用区域并适当拉伸图像。在当前版本中,我从布局中发送一个浮点“比率”参数,尽管最终我打算检测图像的比率
但是,我在可设置样式的属性方面遇到了问题。My attrs.xml具有以下特性:
<resources>
<!-- other stuff, which bizarrely works -->
<declare-styleable name="FixedAspectImageView">
<attr name="ratio" format="float"/>
</declare-styleable>
</resources>
(与末尾带有整数defStyle的布局文件非常相似)我的布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mypackage="http://schemas.android.com/apk/res/com.my.package"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.my.package.view.FixedAspectImageView
android:id="@+id/fixedAspectImageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/picture_of_goatse"
mypackage:ratio="1.2329" />
</FrameLayout>
</LinearLayout>
我已经确定
setRatio(Float)
可以工作,因为如果我在源代码中设置一个值,测量系统就可以完美地工作。传入的TypedArray没有设置索引。这是什么原因造成的?版面编辑器并不总是立即拾取
属性。重新启动eclipse修复了这个问题。上面的代码都是正确的;包名显然应该是布局中命名空间的项目包,自定义视图将使用R.styleable
中生成的值
已知问题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mypackage="http://schemas.android.com/apk/res/com.my.package"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.my.package.view.FixedAspectImageView
android:id="@+id/fixedAspectImageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/picture_of_goatse"
mypackage:ratio="1.2329" />
</FrameLayout>
</LinearLayout>