布局文件:
<LinearLayout android:id="@+id/candi_more_row1" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent"> <View android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="#BDBDBD" android:layout_width="1px" android:layout_height="match_parent"/> <Button android:onClick="onClick" android:textSize="@dimen/candi_box_btn_font_size" android:id="@+id/candi_more_row1_1" android:tag="panelCandiBtn_1" android:text="水电" android:textColor="#474747" android:background="@drawable/keyb_none_bg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> <View android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="#BDBDBD" android:layout_width="1px" android:layout_height="match_parent"/> <Button android:onClick="onClick" android:textSize="@dimen/candi_box_btn_font_size" android:id="@+id/candi_more_row1_2" android:tag="panelCandiBtn_2" android:text="水电" android:textColor="#474747" android:background="@drawable/keyb_none_bg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> <View android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="#BDBDBD" android:layout_width="1px" android:layout_height="match_parent"/> <Button android:onClick="onClick" android:textSize="@dimen/candi_box_btn_font_size" android:id="@+id/candi_more_row1_3" android:tag="panelCandiBtn_3" android:text="水电" android:textColor="#474747" android:background="@drawable/keyb_none_bg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> <View android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="#BDBDBD" android:layout_width="1px" android:layout_height="match_parent"/> <Button android:onClick="onClick" android:textSize="@dimen/candi_box_btn_font_size" android:id="@+id/candi_more_row1_4" android:tag="panelCandiBtn_4" android:text="水电" android:textColor="#474747" android:background="@drawable/keyb_none_bg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> <View android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="#BDBDBD" android:layout_width="1px" android:layout_height="match_parent"/> <Button android:onClick="onClick" android:textSize="@dimen/candi_box_btn_font_size" android:id="@+id/candi_more_row1_5" android:tag="panelCandiBtn_5" android:text="水电" android:textColor="#474747" android:background="@drawable/keyb_none_bg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> <View android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="#BDBDBD" android:layout_width="1px" android:layout_height="match_parent"/> </LinearLayout>
JAVA
LinearLayout lt = (LinearLayout)keyboard_view_zh.findViewById(R.id.candi_more_row1);
for (int i = 0;i < lt.getChildCount(); i++){
if (lt.getChildAt(i) instanceof Button) { // 若是Button记录下
Button tb = (Button)lt.getChildAt(i);
tb.setText("L:"+i);
}
}