2012-02-16 2 views
0

체스 보드를 코딩하고 있습니다. 레이아웃의 경우 여러 병합 레이아웃을 포함합니다. 나는 단순히 속성 코드 (imageView background_color, imageView image path ...)를 변경하기 위해 Activity 코드에서 "병합"된 컴포넌트에 접근하고 싶다.Android API 3 : 병합 된 레이아웃에서 구성 요소에 액세스

편집 : 예를 들어 행 ID (R.id.row_1)를 전달하고 for 루프를 사용하여이 행 ID의 모든 구성 요소를 초기화하려면 8 개의 셀에 각각 다른 배경색이 지정됩니다. 여기

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     initializeComponents(R.id.row_2); 
    } 

public initializeComponents(int id){ 
     /* inflates the given layout child components 
     * and set their properties 
     */ 
} 

은 여기 내 board.xml이 내 main.xml에

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" >  
<include layout="@layout/board"/> 
</LinearLayout> 

입니다

다음
<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<include android:id="@+id/row_8" layout="@layout/board_line"/> 
<include android:id="@+id/row_7" layout="@layout/board_line"/> 
<include android:id="@+id/row_6" layout="@layout/board_line"/> 
<include android:id="@+id/row_5" layout="@layout/board_line"/> 
<include android:id="@+id/row_4" layout="@layout/board_line"/> 
<include android:id="@+id/row_3" layout="@layout/board_line"/> 
<include android:id="@+id/row_2" layout="@layout/board_line"/> 
<include android:id="@+id/row_1" layout="@layout/board_line"/> 

</TableLayout> 

내 board_line.xml :

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 

<TableRow 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
> 
<ImageView 
     android:id="@+id/a" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
    <ImageView 
     android:id="@+id/b" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
    <ImageView 
     android:id="@+id/c" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
    <ImageView 
     android:id="@+id/d" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
    <ImageView 
     android:id="@+id/e" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
    <ImageView 
     android:id="@+id/f" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
    <ImageView 
     android:id="@+id/g" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
    <ImageView 
     android:id="@+id/h" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:minWidth="@dimen/cell_dimension" 
     android:minHeight="@dimen/cell_dimension" 
     android:maxWidth="@dimen/cell_dimension" 
     android:maxHeight="@dimen/cell_dimension" 
    /> 
</TableRow> 

미리 도움을 청하십시오.

+0

이처럼 시도 할 수 있습니다 :'findViewById를 (R.id합니다. row_3) .findViewById (R.id.b)' – zapl

+0

고맙습니다. 그러나 각 자식 구성 요소를 수동으로 언급하지 않고 for/while 루프를 사용하여 선 (R.id.row_2)의 모든 구성 요소를 어떻게 초기화할 수 있는지 알고 있습니까? 미리 감사드립니다. – loloof64

답변

0

LayoutInflater를 사용하거나 완전히 뷰를 프로그래밍 방식으로 만들 수 있으며 모든 것을 사용자 요구에 맞게 조합 할 수 있습니다. 다음 예제에서는 임의의 색상이 지정된 5x5 보드를 만들어야합니다. 코드에서 LayoutParams를 설정하는 것은 추악하기 때문에 XML로 설정하고 레이아웃을 팽창시킵니다.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null); 
    TableLayout tl = (TableLayout) ll.findViewById(R.id.tableLayout); 
    for (int j = 0; j < 5; j++) { 
     TableRow tr = (TableRow) inflater.inflate(R.layout.table_row, null); 
     for (int i = 0; i < 5; i++) { 
      ImageView iv = (ImageView) inflater.inflate(R.layout.image_view, null); 
      iv.setBackgroundColor(new Random().nextInt()); 
      tr.addView(iv); 
     } 
     tl.addView(tr); 
    } 
    setContentView(ll); 
} 

main.xml에

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TableLayout 
     android:id="@+id/tableLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </TableLayout> 
</LinearLayout> 

table_row.xml

<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</TableRow> 

image_view.xml

just one of those you defined in board_line.xml: 
+0

Thnak 매우 대단합니다 :) 나는 그것이 코드로하는 것은 추악한 방법이라고 동의합니다. 그러나 나는 체스 보드를 만들기를 원하기 때문에 여전히 최상의 그래픽 솔루션을 찾고 있습니다 : 전체 코드보기 또는 코드와 레이아웃을 재정 의하여 . (나는 또한 "드래그 앤 드랍"을하고 싶다 : 불행히도 api에서는 사용 가능하지 않다. 3) – loloof64

관련 문제