2012-10-05 7 views
1

나는 Android 3.0 태블릿 응용 프로그램을 개발 중입니다. 프로그래밍프로그래밍 방식으로 레이아웃을 재현 할 수 없습니다.

<LinearLayout 
     android:weightSum="1" 
     android:layout_width="fill_parent" 
     android:layout_height="60dip" 
     android:orientation="horizontal"> 


     <TextView 
      android:id="@+id/textView1" 
      android:layout_weight=".1" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:text="1" /> 


     <TextView 
      android:id="@+id/textView2" 
      android:layout_weight=".6" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:text="Defecto 11" /> 

     <LinearLayout 
      android:gravity="center" 
      android:layout_weight=".05" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" > 

     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" /> 
     </LinearLayout> 

     <LinearLayout 
      android:gravity="center" 
      android:layout_weight=".05" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" > 

     <CheckBox 
      android:id="@+id/checkBox2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" /> 
     </LinearLayout> 


     <LinearLayout 
      android:gravity="center" 
      android:layout_weight=".05" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" > 

     <CheckBox 
      android:id="@+id/checkBox3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" /> 
     </LinearLayout> 


     <LinearLayout 
      android:gravity="center" 
      android:layout_weight=".05" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" > 

     <CheckBox 
      android:id="@+id/checkBox4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" /> 

     </LinearLayout> 


     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".1" 
      android:text="Button" /> 

    </LinearLayout> 

: :이 레이아웃 재현하기 위해 노력하고있어

LinearLayout layout = new LinearLayout(context); 
    LayoutParams parentParams = 
      new LayoutParams(LayoutParams.FILL_PARENT, 60); 
    //parentParams.weight = 1; 
    layout.setOrientation(LinearLayout.HORIZONTAL); 
    layout.setLayoutParams(parentParams); 

    // Parámetros para todos los EditText dentro de la tabla. 
    LayoutParams params = new LayoutParams(0, LayoutParams.FILL_PARENT); 

    // La primera columna es el número del defecto 
    TextView textView = new TextView(context); 
    params.weight = .1f; 
    textView.setLayoutParams(params); 
    textView.setGravity(Gravity.CENTER); 
    textView.setText(Integer.toString(position)); 
    layout.addView(textView); 

    // La segunda columna es la descripción del defecto. 
    params.weight = .6f; 
    textView = new TextView(context); 
    textView.setLayoutParams(params); 
    textView.setGravity(Gravity.CENTER); 
    textView.setText(eDefect.getDescription()); 
    layout.addView(textView); 

    LinearLayout chkLayout = new LinearLayout(context); 
    LayoutParams chkParams = 
      new LayoutParams(0, LayoutParams.FILL_PARENT); 
    chkParams.weight = .05f; 
    chkParams.gravity = Gravity.CENTER; 
    chkLayout.setLayoutParams(chkParams); 

    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    params.gravity = Gravity.CENTER; 
    String eDefectPos = Integer.toString(position); 

    // Columna CRS 
    String tag = eDefectPos + "_CRS"; 
    CheckBox chkBox = new CheckBox(context); 
    chkBox.setLayoutParams(params); 
    chkBox.setTag(tag); 
    chkBox.setOnClickListener(checkListener); 
    chkLayout.addView(chkBox); 
    layout.addView(chkLayout); 

    chkLayout = new LinearLayout(context); 
    chkLayout.setLayoutParams(chkParams); 

    // Columna CRF 
    tag = eDefectPos + "_CRF"; 
    chkBox = new CheckBox(context); 
    chkBox.setLayoutParams(params); 
    chkBox.setTag(tag); 
    chkBox.setOnClickListener(checkListener); 
    chkLayout.addView(chkBox); 
    layout.addView(chkLayout); 

    chkLayout = new LinearLayout(context); 
    chkLayout.setLayoutParams(chkParams); 

    // Columna MA 
    tag = eDefectPos + "_MA"; 
    chkBox = new CheckBox(context); 
    chkBox.setLayoutParams(params); 
    chkBox.setTag(tag); 
    chkBox.setOnClickListener(checkListener); 
    chkLayout.addView(chkBox); 
    layout.addView(chkLayout); 

    chkLayout = new LinearLayout(context); 
    chkLayout.setLayoutParams(chkParams); 

    // Columna MI 
    tag = eDefectPos + "_MI"; 
    chkBox = new CheckBox(context); 
    chkBox.setLayoutParams(params); 
    chkBox.setTag(tag); 
    chkBox.setOnClickListener(checkListener); 
    chkLayout.addView(chkBox); 
    layout.addView(chkLayout); 

    // Boton tomar fotos 
    params = new LayoutParams(0, LayoutParams.WRAP_CONTENT); 
    params.weight = 0.1f; 
    tag = eDefectPos; 
    Button btnTakePhoto = new Button(mActivity); 
    btnTakePhoto.setLayoutParams(params); 
    btnTakePhoto.setTag(tag); 
    btnTakePhoto.setText(getString(R.string.btn_take_photo)); 
    btnTakePhoto.setOnClickListener(takePhotoListener); 
    layout.addView(btnTakePhoto); 

    mLayout.addView(layout, mCurrentDefectTableIndex); 

을 그러나 그들은 동일한 좋아하지 않는다. XML과

이 :

enter image description here

그리고 프로그램이이 :

enter image description here

모든 것이 오른쪽으로 이동했습니다.

단서가 있습니까?

답변

4

다른보기에 대한 LayoutParams에 대한 참조를 다시 사용하고 싶지 않습니다. 재사용을위한 "기본"매개 변수 객체를 만들려면 LayoutParams를 사용하는 LayoutParams 생성자를 사용하여 새로운 참조를 만들어야합니다. 좋아요 :

LayoutParams baseParams = new LayoutParams(0, LayoutParams.FILL_PARENT); 

// La primera columna es el número del defecto 
TextView textView = new TextView(context); 
LayoutParams params1 = new LayoutParams(baseParams); 
params1.weight = .1f; 
textView.setLayoutParams(params1); 
textView.setGravity(Gravity.CENTER); 
textView.setText(Integer.toString(position)); 
layout.addView(textView); 

// La segunda columna es la descripción del defecto. 
LayoutParams params2 = new LayoutParams(baseParams); 
params2.weight = .6f; 
textView = new TextView(context); 
textView.setLayoutParams(params2); 
textView.setGravity(Gravity.CENTER); 
textView.setText(eDefect.getDescription()); 
layout.addView(textView); 
관련 문제