【发布时间】:2017-01-30 10:35:00
【问题描述】:
我是 java 和 xml 编程的新手,我正在使用片段制作选项卡式活动,运行或调试代码时没有错误但 VideoView 没有显示,我正在使用 Android Studio, 这个想法是我想制作选项卡式活动,片段中的 textView 和 videoview 作为可滚动的内容(可能?),还有一件事是 API 级别有影响吗?我的手机还在 KitKat 上 这是(片段)代码:
package com.panduanberwudhu;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.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 = "android.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: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"
tools:context="com.panduanberwudhu.MencuciTangan">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
/>
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/videoplayer"
android:layout_gravity="bottom"
/>
</LinearLayout>
</ScrollView>
【问题讨论】:
-
由于
match_parent属性,您的TextView覆盖了整个布局。 -
保留 android:layout_width="wrap_content" android:layout_height="wrap_content" 并检查一次
-
试过了,我编辑了textview的宽高还是不行,谢谢
标签: java android xml android-studio android-fragments