gpt4 book ai didi

android - 在 ImageView 中显示 ShapeDrawable 时出现问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:42 25 4
gpt4 key购买 nike

我正在编写一个应用程序,它将创建可绘制的形状并将它们添加到其他可绘制对象中。 (将俄罗斯方 block block 想象成俄罗斯方 block 板。)作为测试,我希望在加载 Activity 时显示特定形状,但我无法让 Drawable 出现在我创建的 ImageView 中。谁能解释为什么以下代码不起作用?

Activity 代码:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView mImg = (ImageView) findViewById(R.id.img);

ShapeDrawable sD = new ShapeDrawable(new RectShape());
sD.setBounds(1,1,10,10);
sD.getPaint().setColor(Color.BLUE);

mImg.setImageDrawable(sD);
}

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width="320px"
android:layout_height="206px"
/>
<TextView
android:id="@+id/debug"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

注意:我已经能够使“drawable”文件夹中的静态资源出现在这个 ImageView 中,而不是我在代码中创建的 Drawables。

最佳答案

您还可以使用一个简单的 View ,并使用 setBackgroundDrawable 将其背景可绘制对象设置为形状。

ImageView 可能无法显示您的形状,因为它没有默认大小。您可以使用 sD.setIntrinsicWidthsD.setIntrinsicHeight 为您的形状设置默认大小。

关于android - 在 ImageView 中显示 ShapeDrawable 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2046529/

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