Tôi đang cố tạo một ShapeDrawable theo lập trình nhưng mã sau đây không hiển thị bất kỳ thứ gì.Lập trình tạo ShapeDrawable
ImageView image = new ImageView (context);
image.setLayoutParams (new LayoutParams (200, 200));
ShapeDrawable badge = new ShapeDrawable (new OvalShape());
badge.setBounds (0, 0, 200, 200);
badge.getPaint().setColor(Color.RED);
ImageView image = new ImageView (context);
image.setImageDrawable (badge);
addView (image);
Tôi có thể làm cho nó hoạt động với xml.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="200px"
android:height="200px" />
<solid
android:color="#F00" />
</shape>
ImageView image = new ImageView (context);
image.setLayoutParams (new LayoutParams (200, 200));
image.setImageResource (R.drawable.badge);
addView (image);
Nhưng tôi muốn tạo nó theo chương trình. Các xml hoạt động hoàn hảo do đó vấn đề không thể được với ImageView, nó phải được trong việc tạo ra các ShapeDrawable.
Bạn đã thử đặt giới hạn bố cục cho số lần xem của mình chưa? Vui lòng thêm thông tin về bố cục nơi bạn thêm số lần xem hình ảnh vào –
Điểm này là gì? ImageView image = new ImageView (ngữ cảnh); lần thứ hai? –