En mi aplicación de Android, intento agregar mediante programación una TableRow
vista personalizada ( CustomTableRow
) a un archivo TableLayout
. CustomTableRow
tiene celdas de igual longitud (las dos primeras celdas tienen texto y la tercera tiene un botón de editar y eliminar, como se muestra en el diagrama a continuación):
Sin embargo, cuando agrego programáticamente CustomTableRow
a my TableLayout
, este es el resultado que obtengo (las celdas están todas mezcladas en lugar de ocupar el mismo ancho):
Esto parece extraño, teniendo en cuenta que he especificado que todas las celdas deben tener peso 1 en el diseño XML que infla CustomTableRow. Además, incluso configuré programáticamente las celdas en CustomTableRow para que tengan peso 1 como medida adicional. Y, sin embargo, las células todavía están mezcladas.
A continuación se muestra mi código:
actividad_principal.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/housemate_data_table"
layout="@layout/table" />
</LinearLayout>
tabla.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rent_form"
android:layout_marginTop="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:elevation="4dp"
android:padding="8dp"
android:background="#ffffff"
android:orientation="vertical">
<TableLayout
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TableRow android:background="#d6d7d7" android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Area"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textColor="#000000" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
custom_table_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/col1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="#000000" />
<TextView
android:id="@+id/col2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Area"
android:textColor="#000000" />
<LinearLayout
android:id="@+id/col3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:text="E" />
<Button
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:text="D" />
</LinearLayout>
</TableRow>
MainActivity.java:
package com.example.programaticallyaddtablerowtotablelayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve of the table layout.
TableLayout table = (TableLayout) findViewById(R.id.table_layout);
// Create the housemate's custom TableRow.
CustomTableRow customTableRow = new CustomTableRow(getApplicationContext(), "Name", "Area");
customTableRow.findViewById(R.id.col1).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.findViewById(R.id.col2).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.findViewById(R.id.col3).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
table.addView(customTableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
CustomTableRow.java:
package com.example.programaticallyaddtablerowtotablelayout;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TableRow;
import androidx.annotation.Nullable;
public class CustomTableRow extends TableRow {
public CustomTableRow(Context context) {
super(context);
initUI();
}
public CustomTableRow(Context context, String name, String areaText) {
super(context);
initUI();
}
public CustomTableRow(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initUI();
}
// Initiate the custom TableRow's UI by inflating the template XML layout.
private void initUI() {
inflate(getContext(), R.layout.custom_table_row, this);
}
}
Solución del problema
En custom_table_row.xml
, cambiar TableRow
a la merge
etiqueta resolvió el problema.
No hay comentarios.:
Publicar un comentario