我试图创建一个屏幕(在肖像模式下),显示4个图像(相同的大小,旨在缩小到适合屏幕),占用整个屏幕,将屏幕分解成象限(高2×2网格) 。这将作为主菜单类型的活动,并且每个图像都应该是可点击的,以便让用户参与不同的活动。哪个布局用于2x2基于图像的菜单?
我试过在LinerLayout中使用GridView(使用Google的GridView教程中的很多内容),但无法正确地将图像全部缩放到填充整个屏幕。我在图像周围获得了额外的余量和/或滚动了整个屏幕。
我也尝试过使用TableLayout,在2行中的每一行中放置2个图像。从视觉上来说,这是完美的。不幸的是,当我使用它时,似乎无法在我的活动代码中引用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");
}
});
}
}
我给它一个尝试,我仍然有麻烦。我已经发布了一篇关于我的原文的文章。请参阅上面的编辑1。 – Sam 2010-11-03 19:42:59
我已经找到了findViewById的问题:我忽略了id属性中的一个错误。而不是id =“@ + id/four” 我需要android:id =“@ + id/four”。事情现在运作良好。 – Sam 2010-11-03 20:17:59