2011-11-09 41 views
0

我有一个TableLayout的布局。我把一个TableRow作为标题,里面有3个TextViews。 当我通过代码插入其余行时,对齐方式与第一行不同。tablerows textviews对齐tablelayout

的布局是:

<?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="match_parent"> 
    <TableLayout android:id="@+id/tabla_resumen_equipos" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:stretchColumns="0,1,2"> 
     <TableRow  
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 
      <TextView android:id="@+id/nombre_local" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:gravity="left"/> 

      <TextView android:id="@+id/acciones" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 

      <TextView android:id="@+id/nombre_visitante" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center|right"/> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

用于对准是用于第一行比所述行的其余部分不同。 感谢

编辑:

这是使用的布局的片段:

public class MyFragment extends Fragment { 

    private TableLayout mTableResumenEquipos; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstance){ 
    View v = (LinearLayout)inflater.inflate(R.layout.resumen_equipos, container,false); 
    if (container == null) return null; 
    mTableResumenEquipos = (TableLayout) v.findViewById(R.id.tabla_resumen_equipos); 
    mTvEquipoLocal = (TextView) v.findViewById(R.id.nombre_local); 
    mTvEquipoVisitante = (TextView) v.findViewById(R.id.nombre_visitante); 
    mTvAccion = (TextView) v.findViewById(R.id.acciones); 

    loadData(); 
    return v; 
} 

private void loadData(){ 

    String argsEquipos[] = new String[]{String.valueOf(mIdEquipoLocal), 
      String.valueOf(mIdEquipoLocal), String.valueOf(mIdPartido)}; 
    mDbEquipos = mDbhPlanilla.getReadableDatabase(); 

    Cursor cAcciones = mDbEquipos.rawQuery("SELECT a.accion, a.zonaCampo, a.zonaPorteria, j.equipo " 
      + "FROM Acciones a, Jugadores j WHERE (j.equipo = ? OR j.equipo = ?) " + 
      "AND a.partido = ?",argsEquipos); 

    // Equipos 
    String argLocal[] = new String[]{String.valueOf(mIdEquipoLocal)}; 
    Cursor cEquipoLocal = mDbEquipos.rawQuery("SELECT nombre FROM Equipos WHERE id = ?",argLocal); 
    if(cEquipoLocal.moveToFirst()) mTvEquipoLocal.setText(cEquipoLocal.getString(0)); 

    String argVisitante[] = new String[]{String.valueOf(mIdEquipoVisitante)}; 
    Cursor cEquipoVisitante = mDbEquipos.rawQuery("SELECT nombre FROM Equipos WHERE id = ?",argVisitante); 
    if(cEquipoVisitante.moveToFirst()) mTvEquipoVisitante.setText(cEquipoVisitante.getString(0)); 


    TableRow tr6M = new TableRow(getActivity()); 
    TextView tv6MLocal = new TextView(getActivity()); 
    TextView tv6MNombre = new TextView(getActivity()); 
    TextView tv6MVisitante = new TextView(getActivity()); 

    tv6MLocal.setText(String.valueOf(gol6MLocal)+ "/" + String.valueOf(total6MLocal) + " (" + 
      String.valueOf(porc6MLocal) + ")"); 
    tv6MNombre.setText(getActivity().getString(R.string.seis_metros_corto)); 
    tv6MVisitante.setText(String.valueOf(gol6MVisit)+ "/" + String.valueOf(total6MVisit) + " (" + 
      String.valueOf(porc6MVisit) + ")"); 
    tr6M.addView(tv6MLocal); 
    tr6M.addView(tv6MNombre); 
    tr6M.addView(tv6MVisitante); 
    mTableResumenEquipos.addView(tr6M); 


} 
+0

可能是发布您的代码的好主意 –

回答

2

您需要为您的代码创建视图提供LayoutParams

为您TableRow这将是:

tr6m.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); 

并为您的TextViews这将是:

tv6MLocal.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1)); 
tv6MNombre.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1)); 
tv6MVisitante.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1)); 

最后一个参数是layout_weight这将确保您的TextViews拉伸并占用等量的空间。

+0

感谢您的回答KasperMoerch。我按照你的建议添加了LayoutParams,但我认为我做错了,因为该行已经消失。 – gutiory

+0

尽量不要为'TableRow'设置'LayoutParams' - 仅限于'TextViews'。 – kaspermoerch

+0

你的第一个答案是对的。不需要为TableRow删除。问题在于导入。你可以在这里看到http://stackoverflow.com/questions/3200691/dynamically-add-tablerow-to-tablelayout – gutiory