簡體   English   中英

如何創建表格布局

[英]how to create a table layout

我需要創建一個僅包含兩行兩列的表格布局。目的是創建一個像“百萬富翁”這樣的游戲,玩家可以在其中選擇正確的答案。因此,我將表格布局用於四個答案。 答案顯示為:

  A,C B,D 

如果我回答A但正確答案是C或D,則我有:

http://tinypic.com/view.php?pic=33w33oo&s=5

但是,如果我回答A但正確答案是B,則我有:

http://i40.tinypic.com/5wfg55.jpg

在這種情況下,為什么兩個按鈕會以這種方式出現?

這是我的xml代碼:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res/com.example.applicazionescienza"
    android:id="@+id/tabella"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom|center"
    android:background="@drawable/sfondo"
    android:gravity="bottom|center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Gioca" >

    <TextView
        android:id="@+id/domanda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:alpha="0.8"
        android:background="@drawable/bottonedomanda"
        android:text="TextView" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:gravity="bottom"
        android:shrinkColumns="*"
        android:stretchColumns="*" >

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp" >

            <Button
                android:id="@+id/buttonA"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:alpha="0.6"
                android:background="@drawable/bottonerisposta"
                android:gravity="center"
                android:maxWidth="64dp"
                android:minWidth="64dip"
                android:textSize="11sp"
                android:width="0px" />

            <Button
                android:id="@+id/buttonC"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:alpha="0.6"
                android:background="@drawable/bottonerisposta"
                android:gravity="center"
                android:maxWidth="64dp"
                android:textSize="11sp"
                android:width="0px" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button
                android:id="@+id/buttonB"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:alpha="0.6"
                android:background="@drawable/bottonerisposta"
                android:gravity="center"
                android:maxWidth="64dp"
                android:minWidth="64dip"
                android:textSize="11sp"
                android:width="0px" />

            <Button
                android:id="@+id/buttonD"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:alpha="0.6"
                android:background="@drawable/bottonerisposta"
                android:gravity="center"
                android:maxWidth="64dp"
                android:textSize="11sp"
                android:width="0px" />
        </TableRow>
    </TableLayout>

    <TextView
        android:id="@+id/punteggio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bottonedomanda" />

</TableLayout>

這是我在活動中的代碼,當單擊按鈕A時,將創建一個新的AlertDialog。 這種方式只是單擊按鈕A時的方法。

 if(v.getId()==R.id.buttonA){
            a.setBackgroundResource(R.drawable.bottonerispostaselezionato);
            b.setBackgroundResource(R.drawable.bottonedomanda);
            c.setBackgroundResource(R.drawable.bottonedomanda);
            d.setBackgroundResource(R.drawable.bottonedomanda);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    context);
            builder.setTitle("Conferma");
            builder.setMessage("Vuoi confermare la risposta A?");
            builder.setCancelable(false);

            builder.setNegativeButton("Annulla",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            a.setBackgroundResource(R.drawable.bottonedomanda);
                            b.setBackgroundResource(R.drawable.bottonedomanda);
                            c.setBackgroundResource(R.drawable.bottonedomanda);
                            d.setBackgroundResource(R.drawable.bottonedomanda);
                        }
                    });
            builder.setPositiveButton("Prosegui",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {

                            Integer rispostaEsatta = arrayRisposte
                                    .get(indiceCorrente);
                            if (rispostaEsatta == 0) {//if the correct answer is button A

                                score = score + domandaGiusta;
                                tv.setText("RISPOSTA CORRETTA   PUNTEGGIO: "+score);
                                a.setBackgroundResource(R.drawable.bottonerispostacorretto);// set background green
                                Log.i("Risposta esatta complimenti",
                                        "sdfd");    

                            } else {
                                a.setBackgroundResource(R.drawable.botteerrato);// set background red
                                score = score - domandaSbagliata;
                                tv.setText("RISPOSTA ERRATA   PUNTEGGIO: "+score);              
                                switch (rispostaEsatta) {// if button A is wrong I know who is the correct button...button A=0,B=1,C=2,D=3;
                                case 1:// button B correct but user click on button A
                                    a.setBackgroundResource(R.drawable.botteerrato);

                                    b.setBackgroundResource(R.drawable.bottonerispostacorretto);
//                                  c.setBackgroundResource(R.drawable.bottonedomanda);
//                                  d.setBackgroundResource(R.drawable.bottonedomanda);
                                    Log.i("lunghezza altezza", String.valueOf(a.getWidth())+" "+String.valueOf(a.getHeight()));
                                    Log.i("lunghezza altezza", String.valueOf(b.getWidth())+" "+String.valueOf(b.getHeight()));
                                    Log.i("lunghezza altezza", String.valueOf(c.getWidth())+" "+String.valueOf(c.getHeight()));
                                    Log.i("lunghezza altezza", String.valueOf(d.getWidth())+" "+String.valueOf(d.getHeight()));
                                    break;
                                case 2://Button C correct but user click button A
                                    c.setBackgroundResource(R.drawable.bottonerispostacorretto);
                                    b.setBackgroundResource(R.drawable.bottonedomanda);
                                    d.setBackgroundResource(R.drawable.bottonedomanda);
                                    break;
                                case 3:// button D correct but user click button A
                                    d.setBackgroundResource(R.drawable.bottonerispostacorretto);                                
                                    c.setBackgroundResource(R.drawable.bottonedomanda);
                                    b.setBackgroundResource(R.drawable.bottonedomanda);
                                    break;
                                }

                            } 
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            Thread t=new Thread(){
                                public void run(){
                                    runOnUiThread(new Runnable(){
                                        public void run(){
                                    try {
                                        Thread.sleep(2000);
                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    if(numeroDomande<2){
                                        numeroDomande++;
                                        arrayDomande.remove(indiceCorrente);
                                        arrayRisposte.remove(indiceCorrente);
                                        immaggini.remove(indiceCorrente);

                                        scegliDomanda();


                                    } else {
                                        int v=i+1;
                                        Intent i = new Intent(context,
                                                Livello.class);
                                        i.putExtra("livello", v);
                                        i.putExtra("punteggio",score);
                                        startActivity(i);

                                    }

                                        }
                                    });
                                }
                            };
                            t.start();

                        }
            });

            builder.create().show();

            Log.i("Lo score è ", String.valueOf(score));




        }

請幫我!

好的,在設置布局屬性時會出現一些錯誤。 更具體地說,寬度屬性。 此代碼應該可以解決您的問題。 要了解為什么它確實起作用,請查看表布局Andoird文檔

TableLayout的子級不能指定layout_width屬性。 寬度始終為MATCH_PARENT。 但是,layout_height屬性可以由子級定義。 默認值為WRAP_CONTENT。 如果子級是TableRow,則高度始終為WRAP_CONTENT。

此外,您為按鈕的最大寬度和最小寬度設置了不同的值。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/tabella"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context=".Gioca" >

<TextView
        android:id="@+id/domanda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:alpha="0.8"
        android:text="TextView" />

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:gravity="bottom"
        android:shrinkColumns="*"
        android:stretchColumns="*" >

    <TableRow
            android:id="@+id/tableRow4"
            android:layout_marginBottom="8dp" >

        <Button
                android:id="@+id/buttonA"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:alpha="0.6"
                android:gravity="center"
                android:textSize="11sp"/>

        <Button
                android:id="@+id/buttonC"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:alpha="0.6"
                android:gravity="center"/>
    </TableRow>

    <TableRow
            android:id="@+id/tableRow3"
            android:layout_height="match_parent" >

        <Button
                android:id="@+id/buttonB"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:alpha="0.6"
                android:gravity="center"
                android:textSize="11sp"
                 />

        <Button
                android:id="@+id/buttonD"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:alpha="0.6"
                android:gravity="center"
                android:textSize="11sp"
                />
    </TableRow>
</TableLayout>

<TextView
        android:id="@+id/punteggio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM