【问题标题】:how to create a table layout如何创建表格布局
【发布时间】:2013-05-24 11:43:54
【问题描述】:

我需要创建一个只包含两行两列的表格布局。目的是创建一个像“百万富翁”这样的游戏,玩家可以选择正确的答案。所以我使用表格布局来做四个答案。 答案显示为:

   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。这个cos只是按钮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));




        }

请帮帮我!

【问题讨论】:

    标签: android android-layout android-button android-tablelayout


    【解决方案1】:

    好的,您在设置布局属性时有一些错误。更具体地说,关于宽度属性。此代码应该可以解决您的问题。要了解这究竟为何有效,请查看Table Layout Andoird Documentation

    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>
    

    【讨论】:

    • 你在安卓设备上运行过这段代码吗?我只是用这个布局创建了一个 android 项目,它可以工作。也许,由于您操纵按钮设置为错误或正确的方式,可能会发生此问题。你是怎么做到的?
    • 我根据正确答案修改背景按钮。如果当前按钮是错误答案背景按钮是红色的,如果当前按钮是正确答案背景按钮是绿色的。我已经放了你的 xml 代码,但是如果我选择按钮 A 但正确的答案是按钮 B 我有同样的问题......我已经在帖子中修改了我的代码!
    • @francedema - 如果您需要添加其他详细信息/代码,您可以edit your question
    • 你能附上你的背景图片吗?我认为您的代码是正确的。
    • 我的代码结果在我发布的图片中,就像我帖子中的链接一样!如果我回答 A 但正确答案是 C 或 D,我有这个:tinypic.com/view.php?pic=33w33oo&s=5 但是如果我回答 A 但正确答案是 B,我有这个:i40.tinypic.com/5wfg55.jpg 在这种情况下,为什么两个按钮会出现在那样???
    猜你喜欢
    • 2014-01-15
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    相关资源
    最近更新 更多