android - 无法以编程方式重现布局

标签 android android-layout

我正在开发 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

一切都向右移动。

有什么线索吗?

最佳答案

您不想为不同的 View 重复使用对 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);

关于android - 无法以编程方式重现布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12748500/

相关文章:

android - 可以显示表格的网格吗?

java - Android - 将 pkcs12 证书字符串转换为 bks keystore 的 x509 证书对象

java - 在 Android 中使用 SAXParser 解析 XML 时出现空字符串结果

android - 如何在 Android 的布局 XML 文件中设置 View 的属性?

Android - 可绘制的 XML 包含左侧图像和填充宽度的纯色

php - 将查询结果保存在一个数组中

Android,特殊可绘制对象

android - 如何设置 TimePicker 显示格式为 24 小时

android - 如何通过 TextView 获取 EditText 值并在屏幕上显示?

android - Android 操作栏中 Activity 的中心标题