Android:无法将自定义视图与其他布局一起使用

时间:2018-08-02 14:10:54

标签: android android-layout android-view

我有一个“自定义画布视图”,它仅更改图像。我想在视图下方添加按钮(控件),使其看起来像视频播放器;

问题在于,无论我尝试什么,都无法将控件放在下面。

xml文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:id="@+id/display_video"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Above" />
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:id="@+id/lay"
        android:layout_height="wrap_content">
        <com.example.jdv.myapplication.CanvasView
            android:id="@+id/video_canvas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="#FFFFFF" />
    </FrameLayout>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Below" />

</LinearLayout>

此处“下方的按钮”不显示。

活动

super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_video);
    FrameLayout lay = (FrameLayout) findViewById(R.id.lay);
    LayoutInflater.from(this).inflate(R.layout.activity_display_video, lay);
    customView = new CanvasView(this, null);
    lay.addView(customView);
    onLoad(customView);

我的观点

public class CanvasView extends View {
    private Bitmap image;   //image to be drawn on this view
    private Context context;

    public CanvasView(Context context, AttributeSet attrs) {
        super(context);
        this.context = context;
    }

    public void setNewImage(String filename){
        File file = new File(filename);
        image = BitmapFactory.decodeFile(file.toString());
    }

    public void repaint(){  //method to repaint this view
        this.post(new Runnable(){  
            @Override
            public void run() {
                invalidate(); 
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(image != null)
        canvas.drawBitmap(image, 0, 0, null); //draw the picture in the view
        else {
            Log.d("Image Null", "Cannot Play Image");
        }
    }

我想要这样的东西。图片所在的视图,并在视图下方添加控件。谢谢。 Something like this

0 个答案:

没有答案