有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    使用此布局,其中高度Relativelayoutwrap_content,将根据内容增加/包裹,因为它位于ScrollView

    我删除了Linearlayout作为硬编码的某些值:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mencucitangan_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:id="@+id/ap"
            android:textSize="20sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
    
        <VideoView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="50dp"
            android:foregroundGravity="center"
            android:id="@+id/videoplayer" />
    
    </RelativeLayout>
    </ScrollView>
    

    输出:

    enter image description here