android - 2x2 基于图像的菜单应使用哪种布局?

标签 android layout image-scaling

我正在尝试创建一个屏幕(纵向模式)显示 4 个图像(相同大小,旨在缩小以适合屏幕),占据整个屏幕,将屏幕分成象限(一个高的 2x2 网格) ).这将充当主菜单类型的 Activity ,并且每个图像都应该是可点击的,以便将用户带到不同的 Activity 。

我曾尝试在 LinerLayout 中使用 GridView(大量使用 Google 的 GridView 教程),但无法正确缩放图像以填满整个屏幕。我在图像和/或整个屏幕的滚动周围获得了额外的边距。

我也尝试过使用 TableLayout,在 2 行的每一行中放置 2 个图像。从视觉上看,这非常有效。不幸的是,在使用它时,我似乎无法在我的 Activity 代码中引用 TableLayout 中的 ImageView 项目(findViewById 总是返回 null)。

我觉得 TableLayout 确实不是“正确的做法”,但我想听听其他人的看法。无论哪种方式,应该如何完成我想要的功能?

谢谢。

编辑 1.1: 相对布局更适合让事物排列整齐。现在我只剩下 findViewById 总是返回 null 的问题。到目前为止,这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@color/homescreen_bgcolor"
        >
    <ImageView id="@+id/one"
               android:layout_alignParentTop="true"
               android:layout_alignParentLeft="true"
               android:src="@drawable/item1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"/>
    <ImageView id="@+id/two"
               android:layout_alignParentTop="true"
               android:layout_alignParentRight="true"
               android:src="@drawable/item2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"/>
    <ImageView id="@+id/three"
               android:layout_alignParentBottom="true"
               android:layout_alignParentLeft="true"
               android:src="@drawable/item3"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"/>
    <ImageView id="@+id/four"
               android:layout_alignParentBottom="true"
               android:layout_alignParentRight="true"
               android:src="@drawable/item4"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"/>
    </RelativeLayout>

public class HomeScreenActivity2 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homescreen2);

    ImageView imageView = (ImageView) findViewById(R.id.one);


    imageView.setClickable(true);
    imageView.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        Log.i("Test", "test");
      }
    });
  }
}

最佳答案

这是一个示例布局,展示了如何仅使用 RelativeLayout 实现覆盖整个屏幕的 2 X 2 网格。

<RelativeLayout 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" >

<View
    android:id="@+id/centerVerticalShim"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_centerVertical="true"
    android:visibility="invisible" />

<View
    android:id="@+id/centerHorizontalShim"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:visibility="invisible" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/centerVerticalShim"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/centerHorizontalShim"
    android:background="#42A5F5"
    android:gravity="center"
    android:text="@string/one"
    android:textColor="#FFFFFF" >
</TextView>

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/centerVerticalShim"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/centerHorizontalShim"
    android:background="#EF5350"
    android:gravity="center"
    android:text="@string/two"
    android:textColor="#FFFFFF" >
</TextView>

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/centerVerticalShim"
    android:layout_toLeftOf="@+id/centerHorizontalShim"
    android:background="#66BB6A"
    android:gravity="center"
    android:text="@string/three"
    android:textColor="#FFFFFF" >
</TextView>

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/centerVerticalShim"
    android:layout_toRightOf="@+id/centerHorizontalShim"
    android:background="#5C6BC0"
    android:gravity="center"
    android:text="@string/four"
    android:textColor="#FFFFFF" >
</TextView></RelativeLayout>

上面的布局结果是:

关于android - 2x2 基于图像的菜单应使用哪种布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4089119/

相关文章:

android - 通用图像加载器图像比例

c++ - 通过 OpenGL 进行图像缩放(KeepAspectRatioByExpanding)

android - 检测Android中使用多个MediaCodec作为视频编码器的能力

html - 如何将 div 定位为可见但不计入文档宽度

android - 从另一个 apk 动态加载资源(布局)

opencv - 如何仅放大图像的几个点,图像处理

android - 如何在不调用 Dismiss 的情况下取消 ProgressDialog?

android - 有没有办法让 android 应用程序在手机锁定时响应硬件按钮?

java - Android:创建列表<Map<String, List<ObjectType>>

java - GridBagLayout 每一行都不会改变