【问题标题】:videoview not showing on device nor xml design,videoview 未显示在设备或 xml 设计上,
【发布时间】: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


【解决方案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>

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多