我試圖創建一個屏幕(在肖像模式下),顯示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