android通过gridview填充所有屏幕

时间:2014-01-23 16:15:28

标签: android android-layout android-videoview android-gridview

我有一个gridview并制作一个自定义适配器来填充它,用户可以通过我的网格的微调器行和列来设置。在网格的每个单元格中,我设置了一个视频视图。

所以我需要在我的自定义适配器中为每个视频视图设置dinamically大小,以填充屏幕的剩余部分。关注this我可以执行任务,我获取显示大小并为我的视图设置布局除以行数和列数。

问题是主要布局有动作栏和文本视图。因此,窗口大小不正确。我需要减去动作栏和文本视图大小。 我找到了一个知道动作栏大小的解决方案,但是当我获得textview的高度时,它总是为0。

建议here我应该在渲染后获取textview大小,但是为了渲染我的gridview我需要知道这个大小!!

还有其他方法吗?有必要手动计算视图大小??

这是我的布局

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="it.nexera.visiamobile.ViewLiveMultiActivity"
     >

     <TextView
            android:id="@+id/txt_grid"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="24sp"
           android:textColor="#454545"
           android:gravity="left"
           android:text="@string/sel_grid" />

        <Spinner
            android:id="@+id/grid_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/gridview"
            android:layout_toRightOf="@+id/txt_grid" />

    <GridView  
        android:id="@+id/gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/txt_grid"
        android:gravity="center"
    />

</RelativeLayout>

这是我的自定义适配器的getView方法:

// create a new VideoView for each item referenced by the Adapter
    @SuppressLint("NewApi")
    public View getView(int position, View convertView, ViewGroup parent) {
        VideoView videoView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            videoView = new VideoView(mContext);
                WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            int width=0;
            int height=0;

            if (android.os.Build.VERSION.SDK_INT >= 13){
                Point size = new Point();
                display.getSize(size);
                width = size.x;
                height = size.y;
            }
            else{
                width = display.getWidth();
                height = display.getHeight();

            }
            // Calculate ActionBar height
            TypedValue tv = new TypedValue();
            int actionBarHeight=0;
            if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
            {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,mContext.getResources().getDisplayMetrics());
            }


            TextView textview = (TextView) ((Activity) mContext).findViewById (it.nexera.visiamobile.R.id.txt_grid); 

            int textview_height=textview.getHeight();

            height=height-actionBarHeight-textview_height;
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                    (width/COL_NUMBER),
                    (height/ROW_NUMBER));
            videoView.setLayoutParams(param);



           Uri video = Uri.parse(mvideoSrc[position]);
           videoView.setVideoURI(video);
           videoView.start();

        } else {
            videoView = (VideoView) convertView;
        }

        return videoView;
    }

2 个答案:

答案 0 :(得分:1)

您的问题是您在开始时检索设备整个屏幕的大小。 因此,如果您的布局不占用所有剩余空间,则必须减去ActionBar,但必须减去其他任何视图。所以你的方法与android模块化相矛盾,例如,如果你的视图以不同的方式使用,具体取决于设备的大小。

我认为您需要的是使用ViewTreeObserver,如下所示:

final View myView = ... // The RelativeLayout that's declared as root of your layout file
myView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        // You can here use View.getMeasuredWidth() or View.getMeasuredHeight() which correspond to the available space for the view containing your GridView and your TextView and then set your TextView's size
    }
}

编辑:要使网格视图填充所有剩余空间,您可以使用其权重属性使用LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context="it.nexera.visiamobile.ViewLiveMultiActivity"
     >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_grid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:textColor="#454545"
            android:gravity="left"
            android:text="@string/sel_grid" />

        <Spinner
            android:id="@+id/grid_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <GridView  
        android:id="@+id/gridview"
        android:layout_width="0dp" 
        android:layout_height="0dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:layout_weight="1"
    />    
</LinearLayout>

答案 1 :(得分:0)

您是否尝试过在Grid下添加RelativeLayout,并在其上添加两个隐藏视图,一个在topleft角落,另一个在bottoright角落(例如1dip高度/宽度)。然后,您可以通过代码findViewById将它们调用getLocationOnScreen。这只是我想到的第一个肮脏的想法。这样,您就可以获得Grid的像素(或dpi)的确切大小。