2010-11-03 30 views
1

我试图创建一个屏幕(在肖像模式下),显示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"); 
     } 
    }); 
    } 
} 

回答

0

我觉得一个TableLayout可以为你工作,但我建议你尝试RelativeLayout为好。你基本上可以通过使用

android:layout_alignParentTop="true" 
android:layout_alignParentRight="true" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true"` 

组合图像上的组合,将图像固定到四个象限。

我在我的应用程序中做了类似的事情,在主页上有多个按钮可以启动相应的活动。 RelativeLayout工作正常,并且它避免了嵌套的布局对象,这会妨碍渲染和布局过程中的性能(如果它失控)。

+0

我给它一个尝试,我仍然有麻烦。我已经发布了一篇关于我的原文的文章。请参阅上面的编辑1。 – Sam 2010-11-03 19:42:59

+0

我已经找到了findViewById的问题:我忽略了id属性中的一个错误。而不是id =“@ + id/four” 我需要android:id =“@ + id/four”。事情现在运作良好。 – Sam 2010-11-03 20:17:59

1

下面是一个示例布局,显示如何使用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> 

上述布局的结果是: