按钮消失了

时间:2015-02-17 21:14:21

标签: android tablelayout

我有下一个布局(TOP:两个Spinner Half:TableLayout Down:Button),我用TableService填充了TableLayout ... “for(int i = 0; i< ljsonArray.length(); i ++)”

但是当TableService的最后一条记录显示在TableLayout中时,按钮消失了,出了什么问题?

此致

//Click botton
public void consultar(View v){
    // Building Parameters to call WebService
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    String url = "http://futcho7.com.mx/MiScore/WebService/gettablapos.php";
    params.add(new BasicNameValuePair("id_cliente", numCte.get(0)));
    params.add(new BasicNameValuePair("id_sucursal", numSuc.get(0)));
    params.add(new BasicNameValuePair("id_torneo", Integer.toString(idTorneo)));
    params.add(new BasicNameValuePair("id_jornada", Integer.toString(idJornada)));

    new AsyncConector(this, url, params, this).execute();
}

填充TableLayOut的方法

public void showTablaPos(){
    TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
    //Create table row header 
    TableRow tr_head = new TableRow(this);
    tr_head.setId(10);
    tr_head.setBackgroundColor(Color.GRAY);
    //tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    //Se crean los header
    TextView label_equipo = new TextView(this);
    label_equipo.setId(20);
    label_equipo.setText("EQUIPO");
    label_equipo.setTextColor(Color.WHITE);
    label_equipo.setPadding(5, 5, 5, 5);
    tr_head.addView(label_equipo);

    TextView label_jj = new TextView(this);
    label_jj.setId(20);
    label_jj.setText("JJ");
    label_jj.setTextColor(Color.WHITE);
    label_jj.setPadding(5, 5, 5, 5);
    tr_head.addView(label_jj);

    TextView label_jg = new TextView(this);
    label_jg.setId(20);
    label_jg.setText("JG");
    label_jg.setTextColor(Color.WHITE);
    label_jg.setPadding(5, 5, 5, 5);
    tr_head.addView(label_jg);

    TextView label_je = new TextView(this);
    label_je.setId(20);
    label_je.setText("JE");
    label_je.setTextColor(Color.WHITE);
    label_je.setPadding(5, 5, 5, 5);
    tr_head.addView(label_je);

    TextView label_jp = new TextView(this);
    label_jp.setId(20);
    label_jp.setText("JP");
    label_jp.setTextColor(Color.WHITE);
    label_jp.setPadding(5, 5, 5, 5);
    tr_head.addView(label_jp);

    TextView label_gf = new TextView(this);
    label_gf.setId(20);
    label_gf.setText("GF");
    label_gf.setTextColor(Color.WHITE);
    label_gf.setPadding(5, 5, 5, 5);
    tr_head.addView(label_gf);

    TextView label_ge = new TextView(this);
    label_ge.setId(20);
    label_ge.setText("GE");
    label_ge.setTextColor(Color.WHITE);
    label_ge.setPadding(5, 5, 5, 5);
    tr_head.addView(label_ge);

    TextView label_dif = new TextView(this);
    label_dif.setId(20);
    label_dif.setText("DIF");
    label_dif.setTextColor(Color.WHITE);
    label_dif.setPadding(5, 5, 5, 5);
    tr_head.addView(label_dif);

    TextView label_ptos = new TextView(this);
    label_ptos.setId(20);
    label_ptos.setText("PTOS.");
    label_ptos.setTextColor(Color.WHITE);
    label_ptos.setPadding(5, 5, 5, 5);
    tr_head.addView(label_ptos);

    tablaPos.addView(tr_head, new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    Integer count = 0;
    Integer cuantos = ljsonArray.length();
    try{
        for (int i = 0; i < ljsonArray.length(); i++) {
            ljsonObject = ljsonArray.getJSONObject(i);                  

            String equipo = (ljsonObject.optString("equ_nombre"));
            Integer jj = (ljsonObject.optInt("jj"));
            Integer jg = (ljsonObject.optInt("jg"));
            Integer je = (ljsonObject.optInt("je"));
            Integer jp = (ljsonObject.optInt("jp"));
            Integer gf = (ljsonObject.optInt("gf"));
            Integer ge = (ljsonObject.optInt("ge"));
            Integer dif = (ljsonObject.optInt("dif"));
            Integer pto = (ljsonObject.optInt("puntos"));

            TableRow tr = new TableRow(this);
            if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
            tr.setId(100+count);
            //tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            //Create columns to add as table data
            // Create a TextView to add equipo

            TextView labelEquipo = new TextView(this);
            labelEquipo.setId(200+count); 
            labelEquipo.setText(equipo);
            labelEquipo.setPadding(2, 0, 5, 0);
            labelEquipo.setTextColor(Color.WHITE);
            tr.addView(labelEquipo);

            TextView labelJj = new TextView(this);
            labelJj.setId(200+count); 
            labelJj.setText(Integer.toString(jj));
            labelJj.setPadding(2, 0, 5, 0);
            labelJj.setTextColor(Color.WHITE);
            labelJj.setGravity(Gravity.CENTER);
            tr.addView(labelJj);

            TextView labelJg = new TextView(this);
            labelJg.setId(200+count); 
            labelJg.setText(Integer.toString(jg));
            labelJg.setPadding(2, 0, 5, 0);
            labelJg.setTextColor(Color.WHITE);
            labelJg.setGravity(Gravity.CENTER);
            tr.addView(labelJg);

            TextView labelJe = new TextView(this);
            labelJe.setId(200+count); 
            labelJe.setText(Integer.toString(je));
            labelJe.setPadding(2, 0, 5, 0);
            labelJe.setTextColor(Color.WHITE);
            labelJe.setGravity(Gravity.CENTER);
            tr.addView(labelJe);

            TextView labelJp = new TextView(this);
            labelJp.setId(200+count); 
            labelJp.setText(Integer.toString(jp));
            labelJp.setPadding(2, 0, 5, 0);
            labelJp.setTextColor(Color.WHITE);
            labelJp.setGravity(Gravity.CENTER);
            tr.addView(labelJp);

            TextView labelGf = new TextView(this);
            labelGf.setId(200+count); 
            labelGf.setText(Integer.toString(gf));
            labelGf.setPadding(2, 0, 5, 0);
            labelGf.setTextColor(Color.WHITE);
            labelGf.setGravity(Gravity.CENTER);
            tr.addView(labelGf);

            TextView labelGe = new TextView(this);
            labelGe.setId(200+count); 
            labelGe.setText(Integer.toString(ge));
            labelGe.setPadding(2, 0, 5, 0);
            labelGe.setTextColor(Color.WHITE);
            labelGe.setGravity(Gravity.CENTER);
            tr.addView(labelGe);

            TextView labelDif = new TextView(this);
            labelDif.setId(200+count); 
            labelDif.setText(Integer.toString(dif));
            labelDif.setPadding(2, 0, 5, 0);
            labelDif.setTextColor(Color.WHITE);
            labelDif.setGravity(Gravity.CENTER);
            tr.addView(labelDif);

            TextView labelPtos = new TextView(this);
            labelPtos.setId(200+count); 
            labelPtos.setText(Integer.toString(pto));
            labelPtos.setPadding(2, 0, 5, 0);
            labelPtos.setTextColor(Color.WHITE);
            labelPtos.setGravity(Gravity.CENTER);

            tr.addView(labelPtos);

            // finally add this to the table row
            tablaPos.addView(tr, new TableLayout.LayoutParams(
                                    LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT));
            count++;


        }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
}

XML LayOut

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/verde"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1.14"
        android:text="@string/tit_torneo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blanco"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="0.10"
        android:text="@string/tit_jornada"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blanco"
        android:textStyle="bold" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Spinner
        android:id="@+id/torneo"
        android:layout_width="202dp"
        android:layout_height="wrap_content"
        android:prompt="@string/spinner_prompt_torneo" />

    <Spinner
        android:id="@+id/jornada"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.98" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:layout_weight="1"
    android:orientation="horizontal" >


    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">

        <TableLayout
            android:id="@+id/tabla_pos"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0,1" >

        </TableLayout>
    </ScrollView>

    <Button
        android:id="@+id/consulta"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/blanco"
        android:onClick="consultar"
        android:text="Consultar"
        android:textColor="@color/negro"
        android:textStyle="bold" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您的按钮实际位于tableLayout的右侧。 将linearlayout的方向更改为Vertical而不是horizo​​ntal。

此外,如果你想在scrollview的末尾看到它,buttonView应该是scrollview的一部分。

您还可以在相对布局中嵌套所有xml,并将属性“android:layout_alignParentBottom =”true“添加到按钮,按钮将始终可见。

希望这有帮助。

EDIT-1:

你有没有试过像

这样的东西
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <LinearLayout
        android:background="@color/verde"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1.14"
            android:text="@string/tit_torneo"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/blanco"
            android:textStyle="bold" />

       <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="0.10"
            android:text="@string/tit_jornada"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/blanco"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Spinner
            android:id="@+id/torneo"
            android:layout_width="202dp"
            android:layout_height="wrap_content"
            android:prompt="@string/spinner_prompt_torneo" />

        <Spinner
            android:id="@+id/jornada"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.98" />

     </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:orientation="horizontal" >


        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="vertical">

            <TableLayout
                android:id="@+id/tabla_pos"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="0,1" >

            </TableLayout>
        </ScrollView>


    </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/consulta"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/blanco"
        android:onClick="consultar"
        android:text="Consultar"
        android:textColor="@color/negro"
        android:textStyle="bold"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

当我直接在xml中添加行时,它适用于我。