java videoview未在设备上显示,也未进行xml设计,
我是java和xml编程新手,我正在使用fragment制作选项卡式活动,运行或调试代码时没有错误,但VideoView没有显示,我使用的是Android Studio, 我的想法是,我想制作一个选项卡式活动,将文本视图和视频视图片段作为可滚动的内容(可能吗?),还有一件事是API级别会有影响吗?我的手机还在KitKat上 以下是(片段)代码:
package com.panduanberwudhu;
import 安卓.net.Uri;
import 安卓.support.annotation.Nullable;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.MediaController;
import 安卓.widget.VideoView;
public class MencuciTangan extends Fragment {
private VideoView player;
private String videopath;
private MediaController mediacon;
public View rootView;
//@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.mencucitangan_layout, container, false);
mediacon = new MediaController(getActivity());
player = (VideoView) rootView.findViewById(R.id.videoplayer);
videopath = "安卓.resource://" + getActivity().getPackageName() + "/" + R.raw.washhand;
player.setVideoURI(Uri.parse(videopath));
player.setMediaController(mediacon);
mediacon.setAnchorView(player);
player.start();
return rootView;
//return inflater.inflate(R.layout.mencucitangan_layout,container,false);
}
}
以及布局代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/mencucitangan_layout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.panduanberwudhu.MencuciTangan">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/app_name"
/>
<VideoView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/videoplayer"
安卓:layout_gravity="bottom"
/>
</LinearLayout>
</ScrollView>
# 1 楼答案
使用此布局,其中高度
Relativelayout
为wrap_content
,将根据内容增加/包裹,因为它位于ScrollView
内我删除了
Linearlayout
作为硬编码的某些值:输出: