VideoView在Linearlayout

时间:2015-07-22 10:47:50

标签: android layout android-videoview

我在Activity中有一个Fragment,在片段里面我有2个Linear Layouts。第一个布局包含一个VideoView,我希望VideoView适合屏幕的宽度,线性布局要包裹它的高度(VideoView)。然后,我希望第二个LinearLayout调整屏幕的其余高度。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <LinearLayout
            android:id="@+id/viewA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_purple"
            android:orientation="vertical">

            <!-- Aqui ira el VideoView -->
            <VideoView
                android:id="@+id/video"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:tag="main"/>

            <!--<ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/entreno3"/>-->

        </LinearLayout>

        <LinearLayout
            android:id="@+id/viewB"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_orange_light"
            android:orientation="vertical">

                <!-- Aqui iran los datos del ejercicio -->

        </LinearLayout>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_fast_forward"
        app:layout_anchor="@id/viewA"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

我得到这个代码的结果很好,但我有一个问题,问题是当加载这个片段时,第二个LinearLayout被粘在黑色屏幕上并且浮动按钮暂时没有很好定位,然后在半秒钟内,第二个线性布局和按钮显示在正确的位置。

我尝试使用ImageView而不是VideoView,因为它已加载,所以它的位置很好。这是我的java代码。

public class Ejercicio extends Fragment {

    private final static String uriPath = "android.resource://com.fitness.dullmonkey.keepingfit/raw/";

    public Ejercicio() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_ejercicio, container, false);

        VideoView video = (VideoView) view.findViewById(R.id.video);
        String path = uriPath + "ejercicio1";
        video.setVideoPath(path);
        video.start();


        Uri uri = Uri.parse(path);
        video.setVideoURI(uri);

        // Inflate the layout for this fragment
        return view;
    }
}

这是我加载片段和加载半秒后的结果。

http://es.tinypic.com/view.php?pic=9hnuqt&s=8#.Va92lfntlBc

调试后我注意到产生错误的是XML中的FloatingActionButton视图。

你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

我发现的最佳方法是将按钮放在屏幕底部并在Java代码中设置

video.setZOrderOnTop(true);

我必须将按钮放在底部,因为如果我设置了video.setZOrderOnTop(true);视频显示在按钮上。