我正在製作一個應用程序中的imageview的英文字母,我想知道什麼是做這件事的最佳方式?Android Studio Mass ImageView陣列
注意:字母表中有26個字母! 什麼,我需要做的:
充分利用ImageView的
設置它的背景
設置ID
讓它可點擊
添加點擊監聽,每一個人形象查看
目前,我有以下代碼(是的,我知道這是很業餘)
public class Main_game extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
LinearLayout l = (LinearLayout)findViewById(R.id.linearLetters);
ImageView[] letters = new ImageView[26];
letters[1] = (ImageView)findViewById(R.id.i1);
letters[1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do the task
}
});
letters[2] = (ImageView)findViewById(R.id.i2);
letters[3] = (ImageView)findViewById(R.id.i3);
letters[4] = (ImageView)findViewById(R.id.i4);
letters[5] = (ImageView)findViewById(R.id.i5);
letters[6] = (ImageView)findViewById(R.id.i6);
letters[7] = (ImageView)findViewById(R.id.i7);
letters[8] = (ImageView)findViewById(R.id.i8);
letters[9] = (ImageView)findViewById(R.id.i9);
letters[10] = (ImageView)findViewById(R.id.i10);
letters[11] = (ImageView)findViewById(R.id.i11);
letters[12] = (ImageView)findViewById(R.id.i12);
letters[13] = (ImageView)findViewById(R.id.i13);
letters[14] = (ImageView)findViewById(R.id.i14);
letters[15] = (ImageView)findViewById(R.id.i15);
letters[16] = (ImageView)findViewById(R.id.i16);
letters[17] = (ImageView)findViewById(R.id.i17);
letters[18] = (ImageView)findViewById(R.id.i18);
letters[19] = (ImageView)findViewById(R.id.i19);
letters[20] = (ImageView)findViewById(R.id.i20);
letters[21] = (ImageView)findViewById(R.id.i21);
letters[22] = (ImageView)findViewById(R.id.i22);
letters[23] = (ImageView)findViewById(R.id.i23);
letters[24] = (ImageView)findViewById(R.id.i24);
letters[25] = (ImageView)findViewById(R.id.i25);
letters[26] = (ImageView)findViewById(R.id.i26);
}
}
我的佈局設置,像這樣:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/linearLetters">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i1"
android:background="@drawable/a1"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i2"
android:background="@drawable/a2"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i3"
android:background="@drawable/a3"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i4"
android:background="@drawable/a4"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i5"
android:background="@drawable/a5"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i6"
android:background="@drawable/a6"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i7"
android:background="@drawable/a7"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i8"
android:background="@drawable/a8"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i9"
android:background="@drawable/a9"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i10"
android:background="@drawable/a10"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i11"
android:background="@drawable/a11"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i12"
android:background="@drawable/a12"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i13"
android:background="@drawable/a13"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i14"
android:background="@drawable/a14"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i15"
android:background="@drawable/a15"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i16"
android:background="@drawable/a16"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i17"
android:background="@drawable/a17"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i18"
android:background="@drawable/a18"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i19"
android:background="@drawable/a19"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i20"
android:background="@drawable/a20"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i21"
android:background="@drawable/a21"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i22"
android:background="@drawable/a22"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i23"
android:background="@drawable/a23"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i24"
android:background="@drawable/a24"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i25"
android:background="@drawable/a25"
android:clickable="true" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/i26"
android:background="@drawable/a26"
android:clickable="true" />
</LinearLayout>
</HorizontalScrollView>
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/game_tabhost"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/gameTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/hangman_0"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="110dp"
android:layout_marginLeft="100dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/helpTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/helptab_layout"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
有沒有更簡單的方法來做到這一點?
我已經嘗試了使數組的等等,但似乎沒有工作正常。對不起,這是一個非常複雜的問題。
你是怎麼得到這麼快!我厭倦了人們所做的不同循環,這一切都讓我感到困惑。 – ElectroMan
我希望我不會讓你困惑:)只要問我是否想讓我澄清一步。 –
沒有什麼讓我困惑的是'int res = getResources()。getIdentifier(「i」+ i,「id」,getPackageName());'因爲我不知道該怎麼做。 – ElectroMan