【问题标题】:How to display a video in a CardView on Android?如何在 Android 上的 CardView 中显示视频?
【发布时间】:2015-08-12 19:07:40
【问题描述】:

我对 Android 还很陌生,最近我在我的应用程序中添加了一个 CardView 来显示一些文本框和图像,效果很好。

但是,当我尝试显示视频时,卡仍然是空的。有人可以帮我理解我做错了什么吗?

我已经像这样定义了ImageView

<android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/card_margin"
                android:layout_marginLeft="@dimen/card_margin"
                android:layout_marginRight="@dimen/card_margin">
                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                        <VideoView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:id="@+id/step1Vid" />
                        <MediaController
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/mediaController2"
                            android:layout_below="@id/step1Vid" />

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/step_image"/>
               </LinearLayout>
            </android.support.v7.widget.CardView>

最初我尝试在相关活动的 onCreate 中设置视频,如下所示:

VideoView video = (VideoView) findViewById(R.id.step1Vid);
    Uri vidUri = Uri.parse(SOME_MP4_URL);
    video.setVideoURI(vidUri);
    MediaController mCon = new MediaController(getApplicationContext());
    mCon.setAnchorView(video);
    video.setMediaController(mCon);

这不起作用,不知道为什么。所以然后从相关的Activity 我使用AsyncTask 从URL 获取视频。由于 UI 更改也不起作用,因此在 onProgressMethod 中使用了相同的代码。

如果我能得到任何建议,我将不胜感激。

【问题讨论】:

  • 请提供您的异步任务代码
  • 显示相关代码,不仅仅是布局

标签: android android-videoview android-cardview


【解决方案1】:

当我给 LinearLayout 一个固定大小而不是使用“wrap_content”属性时,问题就解决了。

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 2017-11-17
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多