哪种布局适用于 2x2 基于图像的菜单?
我正在尝试创建一个屏幕(纵向模式),显示 4 个图像(大小相同,旨在缩小以适合屏幕),占据整个屏幕,将屏幕分成象限(一个高的 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");
}
});
}
}
I am trying to create a screen (in portrait mode) that shows 4 images (same size, intended to scale down to fit screen), taking up the entire screen, breaking up the screen into quadrants (a tall, 2x2 grid). This will act as a main menu type of activity and each image should be clickable, in order to take the user to a different activity.
I have tried using a GridView inside a LinerLayout (using a lot from Google's GridView tutorial) but cannot get the images to all scale properly to fill the entire screen. I get extra margins around the images and/or scrolling of the entire screen.
I have also tried using a TableLayout, placing 2 images in each of the 2 rows. Visually, that worked perfectly. Unfortunately when using that, I cannot seem to reference the ImageView items in the TableLayout in my activity code (findViewById always returns null).
I feel like a TableLayout is really not the "right thing to do" but I would like to hear what others have to say. Either way, what should be done to accomplish my desired functionality?
Thanks.
Edit 1.1:
The relative layout works much better for getting things lined up. Now I'm just left with the issue where findViewById always returns null. Here is my code so far:
<?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");
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个示例布局,展示了如何仅使用relativelayout 实现覆盖整个屏幕的 2 x 2 网格。
上面的布局结果是这样的:
Here is a sample layout showing how you can achieve a 2 X 2 grid that covers the entire screen using just a RelativeLayout.
The above layout results in this:
我认为 TableLayout 可以为您工作,但我建议尝试 RelativeLayout 也是如此。 的组合将图像固定到四个象限
您基本上可以通过使用图像上
。我正在我的应用程序中执行类似的操作,主页上有多个可以启动相应活动的按钮。 relativeLayout 工作得很好,并且它避免了嵌套的 Layout 对象,这可能会影响渲染和布局过程中的性能(如果它失控)。
I think a TableLayout could work for you, but I'd recommend trying out RelativeLayout as well. You can basically pin your images to the four quadrants by using combinations of
on your images.
I'm doing something similar in my app where I have multiple buttons on a homepage that can launch corresponding activities. RelativeLayout works fine, and it avoids nested Layout objects, which can hamper performance during render and layout procedures (if it gets out of hand).