【问题标题】:android fill all screen by gridviewandroid通过gridview填充所有屏幕
【发布时间】:2014-01-24 21:32:36
【问题描述】:

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

所以我需要在我的自定义适配器中为每个视频视图动态设置大小,以填充屏幕的剩余部分。在this 之后,我可以完成任务,我将显示大小和布局设置为我的视图除以行数和列数。

问题是主要布局有 action-bar 和 textview 。因此,窗口大小不正确。我需要减去操作栏和文本视图的大小。 我找到了一个知道操作栏大小的解决方案,但是当获取我的 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;
    }

【问题讨论】:

  • fill_parent已弃用,请改用match_parent
  • 好的,谢谢,但这不能解决我的问题

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


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

【讨论】:

  • 好的,但我需要调整 videoview 的大小,而不是 textview...如何在嵌套的 ongloballayoutlistener 中做到这一点?
  • gridview 的一些不错的特性是它会自动调整其子视图的大小,因此,通过设置 gridview 的大小,内部包含的视频视图应相应地调整大小。如果这不能满足您的要求,您能否准确说明问题所在?
  • 是的,我希望 gridview 填满剩余的整个屏幕。但是 Gridview 本身不做这件事,我必须为孩子手动设置布局,在这种情况下是 videoviews。因为如果我设置 videoView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));在 getView 方法中,视频未显示。仅当我为布局参数设置固定宽度和高度时,视频才会显示。如果您可以发布自动 gridview 布局的示例,我将不胜感激
  • 好的。据我了解,您的 TextView 以某种方式位于 gridview 之上,并且您希望 gridview 占用所有剩余空间。我发布了另一个解决这个问题的答案
  • 采用这种布局,视频不会出现。并且 layout_above 和 toRightOf 不能与 LinearLayout 一起使用(这没关系..)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多