Tôi muốn thêm một VideoView vào LinearLayout theo chương trình. LinearLayout có id là "main".Thêm VideoView vào bố cục XML
Tham chiếu câu hỏi SO Video Streaming and Android này, tôi có thể hiển thị và phát video, nhưng điều gì sẽ xảy ra nếu tôi muốn tạo VideoView mới và thêm nó vào bố cục?
Đây là XML Tôi cố gắng để "sao chép" cách lập trình:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent">
<VideoView android:id="@+id/your_video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
Sau đây là cách tôi đã cố gắng để thực hiện điều tương tự cách lập trình:
VideoView videoView = new VideoView(this);
LinearLayout layout = (LinearLayout)findViewById(R.id.main);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(videoView, params);
Vấn đề là video doesn 't hiển thị ở tất cả! Với VideoView được khai báo trong XML, mọi thứ đều hoạt động tốt, nhưng theo lập trình, nó không hoạt động chút nào.
Để rõ ràng, tôi không muốn có một VideoView được xác định trong tệp XML khi tôi thực hiện nó theo lập trình.
Khi bạn gọi 'addView() 'và không có gì xảy ra, không logcat nói gì? –
Không, logcat không nói bất cứ điều gì – user5243421