gpt4 book ai didi

android - 难以使用 AttributeSet.getAttributeIntValue()

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:09:03 24 4
gpt4 key购买 nike

我的应用需要扩展 ImageView 的高度和宽度。我认为这应该有效:

package my.android.test;

public class TestImageView extends ImageView
{
private int mWidth=0;
private int mHeight=0;

public TestImageView(Context context, AttributeSet as)
{
super(context, as);
final String xmlns="http://schemas.android.com/apk/res/android";
mWidth = as.getAttributeIntValue(xmlns, "layout_width", 0);
mHeight = as.getAttributeIntValue(xmlns, "layout_height", 0);
}

@Override
protected void onDraw(Canvas canvas)
{
//do stuff
}
}

但它不起作用。 0 由 getAttributeIntValue() 返回,尽管 layout_height 是在布局 XML 中定义的:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="#fffcb95a"

<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

<FrameLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"

<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"

<my.android.test.TestImageView
android:id="@+id/TestImageViewID"
android:layout_width="100dip"
android:layout_height="100dip"
android:src="@drawable/icon"
android:scaleType="centerCrop" />

</RelativeLayout

</FrameLayout

</LinearLayout
</ScrollView

怎么办?

最佳答案

请注意为什么最初的尝试不起作用的解释 -- layout_width 和 layout_height 实际上不是保存整数,而是保存尺寸。您需要通过 Context/Theme.obtainStyledAttributes 检索它们,这样您就可以将它们作为可以转换为像素的 TypedValue 获取。

关于android - 难以使用 AttributeSet.getAttributeIntValue(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com