【问题标题】:Android main_activity simple Games pattern e.g. for CardgameAndroid 主要活动简单游戏模式,例如对于纸牌游戏
【发布时间】:2014-04-29 19:27:10
【问题描述】:

我从没想过要完成这件事会遇到这么多麻烦。

我想显示内容,比如说 2 行 4 列。 每个单元格中都有一张不同大小和不同纵横比的图片。 所有 8 个单元格应填满整个屏幕。

我现在想在分辨率为 1920x1080 像素的智能手机和分辨率为 480x320 像素的智能手机上正确显示(缩小/缩放)这 8 张图像。 在这两种情况下,8 张图片都应该填满整个屏幕。

谁能这么好心,贴出这几行代码?

或者我是否需要以编程方式完成这一切,检查屏幕分辨率,检查 dpi,然后重新缩放每个图像?

非常感谢

这对我不起作用:

    <GridLayout
        android:id="@+id/gridLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:rowCount="2" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left|top"
            android:src="@drawable/img1" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left|top"
            android:src="@drawable/img2" />
    </GridLayout>

我尝试了几乎任何类型的组合使用

android:layout_width="match_parent" fill_parent 或 wrap_content

【问题讨论】:

  • 使用设置的行数和列数进行 GridLayout,并将布局参数设置为 match_parent 的宽度和高度。只是一种选择。有几种方法可以做到这一点
  • 能否请您发布一些代码,我就是这样做的,但它根本不起作用。 LinearLayout 顶部应该是什么?相对布局? s14.directupload.net/images/140429/y6ooprxs.jpg

标签: android grid android-activity


【解决方案1】:

所以我终于开始着手处理这个问题了,你是对的,因为某种原因,GridLayout 似乎有点不对劲。但是,我编写并测试了以下代码,它可以满足您的需要。显然,您只需将图像更改为您需要的。

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="vertical"
    android:id="@+id/grid">

    <RelativeLayout
        android:id="@+id/top_row"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1"
        android:orientation="horizontal" />

    <RelativeLayout
        android:id="@+id/bottom_row"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1"
        android:orientation="horizontal" />

</LinearLayout>   

然后你可以动态添加你的视图

RelativeLayout topLayout = (RelativeLayout) findViewById(R.id.top_row);
RelativeLayout bottomLayout = (RelativeLayout) findViewById(R.id.bottom_row);

Point point = new Point();
getWindowManager().getDefaultDisplay().getSize(point);
int width = point.x;
RelativeLayout.LayoutParams params;

    for(int i = 0; i < 8; i++){
        params = new RelativeLayout.LayoutParams(width/4, RelativeLayout.LayoutParams.MATCH_PARENT);
        ImageView image = new ImageView(this);
        image.setId(i+1);
        image.setVisibility(View.VISIBLE);
        image.setImageResource(R.drawable.ic_launcher);
        image.setScaleType(ScaleType.FIT_XY);

        if(i < 4){
            if(i == 0)
                params.addRule(RelativeLayout.ALIGN_LEFT);
            else
                params.addRule(RelativeLayout.RIGHT_OF, i);

            image.setLayoutParams(params);
            topLayout.addView(image);
        }
        else if(i >=4){
            if(i == 4)
                params.addRule(RelativeLayout.ALIGN_LEFT);
            else
                params.addRule(RelativeLayout.RIGHT_OF, i);

            image.setLayoutParams(params);
            bottomLayout.addView(image);
        }

    }

应该按照你的要求去做:)

【讨论】:

  • 哇,这真是太神奇了:) 它完全符合我的要求。谢谢
猜你喜欢
  • 2011-07-01
  • 1970-01-01
  • 2017-12-17
  • 2011-08-02
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
相关资源
最近更新 更多