代码似乎改变了我的布局文件

时间:2014-02-08 16:58:15

标签: android xml layout tablelayout tablerow

我有四个TableRows的TableLayout和四行中的四个按钮。 我有不同屏幕分辨率的大问题,所以我建立了我的布局文件新。

首先,我给你我的代码:

public class MainActivity extends Activity {

    Button b,b2,b3,b4, b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;
    //b21,b22,b23,b24;


    ArrayList<Button> arr = new ArrayList<Button>();

    MediaPlayer mp;
    Button aktuell;
    TextView tv;
    String playtone ="";
    String shouldvibra ="";
    TextView tvPunktestand;
    int count = 0;
    CountDownTimer countdown;
    CountDownTimer eins;
    CountDownTimer zwei;
    CountDownTimer drei;
    AlertDialog.Builder alertDialog;
    boolean einsstarted = false;
    boolean zweistarted = false;
    boolean dreistarted = false;
    boolean countdownstarted = false;
    SharedPreferences app_preferences;
     Vibrator vibe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         vibe = (Vibrator) this.getSystemService(this.VIBRATOR_SERVICE) ;

        b = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        b5 = (Button) findViewById(R.id.button5);
        b6 = (Button) findViewById(R.id.button6);
        b7 = (Button) findViewById(R.id.button7);
        b8 = (Button) findViewById(R.id.button8);
        b9 = (Button) findViewById(R.id.button9);
             b10 = (Button) findViewById(R.id.button10);
             b11 = (Button) findViewById(R.id.button11);
             b12 = (Button) findViewById(R.id.button12);
            b13 = (Button) findViewById(R.id.button13);
            b14 = (Button) findViewById(R.id.button14);
            b15 = (Button) findViewById(R.id.button15);
            b16 = (Button) findViewById(R.id.button16);
           b17 = (Button) findViewById(R.id.button17);
           b18 = (Button) findViewById(R.id.button18);
           b19 = (Button) findViewById(R.id.button19);
           b20 = (Button) findViewById(R.id.button20);
          // b21 = (Button) findViewById(R.id.button21);
          // b22 = (Button) findViewById(R.id.button22);
           //b23 = (Button) findViewById(R.id.button23);
         //  b24 = (Button) findViewById(R.id.button24);
           //tvPunktestand = (TextView) findViewById(R.id.tvPunktewaehrend);

//        b2.setVisibility(View.GONE);
//        b3.setVisibility(View.GONE);
//        b4.setVisibility(View.GONE);
//        b5.setVisibility(View.GONE);
//        b6.setVisibility(View.GONE);
//        b7.setVisibility(View.GONE);
//        b8.setVisibility(View.GONE);
//        b9.setVisibility(View.GONE);
//        b10.setVisibility(View.GONE);
//        b11.setVisibility(View.GONE);
//        b12.setVisibility(View.GONE);
//        b13.setVisibility(View.GONE);
//        b14.setVisibility(View.GONE);
//        b15.setVisibility(View.GONE);
//       b16.setVisibility(View.GONE);
//        b17.setVisibility(View.GONE);
//        b18.setVisibility(View.GONE);
//        b19.setVisibility(View.GONE);
//        b20.setVisibility(View.GONE);
//        b21.setVisibility(View.GONE);
//        b22.setVisibility(View.GONE);
//        b23.setVisibility(View.GONE);
//        b24.setVisibility(View.GONE);

        arr.add(b);
        arr.add(b2);
        arr.add(b3);
        arr.add(b4);
        arr.add(b5);
        arr.add(b6);
        arr.add(b7);
        arr.add(b8);
        arr.add(b9);
        arr.add(b10);
        arr.add(b11);
        arr.add(b12);
        arr.add(b13);
        arr.add(b14);
        arr.add(b15);
        arr.add(b16);
        arr.add(b17);arr.add(b18);
        arr.add(b19);
        arr.add(b20);
        //arr.add(b21);


        //arr.add(b22);arr.add(b23);
        //arr.add(b24);

         int boeserzufall = (int) (Math.random()*23);
         Button boese = arr.get(boeserzufall);
         arr.get(boeserzufall).setBackgroundResource(R.drawable.williboese);

         arr.get(boeserzufall).setTag(R.drawable.williboese);

         System.out.println("AAAAAAAAAAAAAAAAAAAAAA: " + boese.getTag());






         alertDialog = new AlertDialog.Builder(this);  
         alertDialog.setTitle("Countdown - gleich geht´s los");  
         alertDialog.setMessage("");
         alertDialog.setCancelable(false);

         final AlertDialog ag = alertDialog.show();

         final TextView myMsg = (TextView)ag.findViewById(android.R.id.message);
         myMsg.setGravity(Gravity.CENTER);
         ag.show();

         countdown =  new CountDownTimer(4000, 1000) {
             @Override
             public void onTick(long millisUntilFinished) {
                 countdownstarted = true;
                 shared_check();
                 shouldvibra = app_preferences.getString("vibra", "Fehler");
                myMsg.setText(" " + (millisUntilFinished) / 1000);
                if(shouldvibra.equals("j") || shouldvibra == null){
                    vibe.vibrate(100);
                }
             }

             @Override
             public void onFinish() {
                 ag.dismiss();

                 playtone = app_preferences.getString("ton", "Fehler");

                 if(playtone.equals("j") || playtone == null){
                     playmusic();
                 }




         eins = new CountDownTimer(10000, 790) {

            @Override
            public void onTick(long millisUntilFinished) {

                for(int i = 0; i< arr.size(); i++){
                    Button aga = arr.get(i);
                    if(aga.getVisibility() == View.VISIBLE){
                        aga.setVisibility(View.GONE);
                    }
                }
                einsstarted = true;

                int zufall = (int) (Math.random()*19);
                setNextButton(arr.get(zufall));
                //tvPunktestand.setText("" + count);
                System.out.println("HHH");
            }

            @Override
            public void onFinish() {

                //Intent highscore = new Intent (MainActivity.this, Highscore_eintragen.class);
               // highscore.putExtra("count", count);
               // startActivity(highscore);
                 System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");


                  zwei = new CountDownTimer(10000, 550) {

                    @Override
                    public void onTick(long millisUntilFinished) {
                        for(int i = 0; i< arr.size(); i++){
                            Button aga = arr.get(i);
                            if(aga.getVisibility() == View.VISIBLE){
                                aga.setVisibility(View.GONE);
                            }
                        }
                        zweistarted = true;
                        int zufall = (int) (Math.random()*19);
                        setNextButton(arr.get(zufall));
                        //tvPunktestand.setText("" + count);
                        System.out.println("HHH");
                    }

                    @Override
                    public void onFinish() {

                       // Intent highscore = new Intent (MainActivity.this, Highscore_eintragen.class);
                        //highscore.putExtra("count", count);
                        //startActivity(highscore);
                         System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                        drei = new CountDownTimer(10000, 350) {

                            @Override
                            public void onTick(long millisUntilFinished) {
                                for(int i = 0; i< arr.size(); i++){
                                    Button aga = arr.get(i);
                                    if(aga.getVisibility() == View.VISIBLE){
                                        aga.setVisibility(View.GONE);
                                    }
                                }
                                dreistarted = true;
                                int zufall = (int) (Math.random()*19);
                                setNextButton(arr.get(zufall));
                                //tvPunktestand.setText("" + count);
                                System.out.println("HHH");
                            }

                            @Override
                            public void onFinish() {
                               System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                Intent highscore = new Intent (MainActivity.this, NachdemSpiel.class);
                                highscore.putExtra("count", count);
                                startActivity(highscore);
                                beenden();


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




        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }

        });


        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b2.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
                }
        });



        b3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b3.setBackgroundColor(R.color.redwue);
                count++;

                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }

            }
        });


        b4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b4.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }

            }
        });

        b5.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b5.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }

            }
        });

        b6.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b6.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b7.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b7.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b8.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b8.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b9.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b9.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b10.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b10.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b11.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b11.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b12.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b12.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b13.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b13.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b14.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b14.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b15.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b15.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b16.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b16.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b17.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b17.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b18.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b18.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
       b19.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b19.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b20.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b20.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
//        b21.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });
//        b22.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });
//        b23.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });
//        b24.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });


    }  

    @Override
    public void onBackPressed() { 
        System.out.println("asdhaskjdhaskjdhakjsdhkjasdhkjahsdkjashdkjh");
         Intent i = new Intent(MainActivity.this, MainMenu.class);
         startActivity(i);

         if( countdownstarted == true){
             countdown.cancel();
         }
         if(einsstarted == true){
             eins.cancel(); 
             if(playtone.equals("j")){
                 stopmusic();
             }
         }
         if(zweistarted == true){
             zwei.cancel(); 
             if(playtone.equals("j")){
                 stopmusic();
             }
         }
         if(dreistarted == true){
             drei.cancel();
             if(playtone.equals("j")){
                 stopmusic();
             }
         }
         this.finish();

    }


    public void showmessage(){
        Toast.makeText(MainActivity.this, "Oh nein... Du hast den Kaktus erwischt! Alle deine Punkte sind wieder weg.", Toast.LENGTH_SHORT).show();
    }
    public void setNextButton(Button str){

        System.out.println("&&&&&&&&&&& SETNEXTBUTTON");
        //str.setVisibility(ImageButton.GONE);

        int buttonid = str.getId(); 
        int buttonname = str.getId();
        //String buttonneu = "button" + zufall; 
        System.out.println("&&&&&&&&&&&" +  getResources().getResourceEntryName(buttonid));
        int[] buttonIds = {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8, 
                R.id.button9, R.id.button10, R.id.button11, R.id.button12, R.id.button13, R.id.button14, R.id.button15, R.id.button16 , 
                R.id.button17, R.id.button18, R.id.button19, R.id.button20};
                //,R.id.button21, R.id.button22, R.id.button23, R.id.button24 

        //Button bnew = (Button) findViewById(buttonIds[zufall]);
        //Toast.makeText(this, "$ " + getResources().getResourceEntryName(bnew.getId()), Toast.LENGTH_LONG).show();
        //if(bnew.getId() == str.getId()){
        //   bnew = (Button) findViewById(buttonIds[zufall]);

        //}


        str.setVisibility(View.VISIBLE);  


        //b.setEnabled(enabled)
    }
    public void beenden(){
        this.finish();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void playmusic(){
         mp = MediaPlayer.create(getApplicationContext(), R.raw.music);
        mp.start();
    }
    public void stopmusic(){
      mp.stop();    
    }


    public boolean shared_check(){
        boolean gibtswelche = false;
        String name;
        app_preferences = getSharedPreferences("wuestenfest", 0);
        SharedPreferences.Editor editor = app_preferences.edit(); 

        String tonvalue = app_preferences.getString("ton",null);
        String vibravalue = app_preferences.getString("vibra", null);
        if (tonvalue == null && vibravalue == null) {
            System.out.println(" Keine Shared Preferences gefunden");
        } else {
            app_preferences.getString("ton", "testasd");
            System.out.println("Shared Preferences gefunden");
            gibtswelche = true;
        }
        return gibtswelche;
    }
    public void vibrieren(){
        vibe.vibrate(50);
    }

}

就像你可能看到的那样,我的目标是设置一个按钮可见,其他每个按钮都不可见。每隔几毫秒,这应该改变。

这是我的布局文件:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:stretchColumns="0,1,2,3,4,5"


    tools:context=".MainActivity" >

    <TableRow 
        android:id="@+id/tableRow1"

        android:paddingTop="22dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/button1"
            android:text="1" 
            android:background="@drawable/willi"> 
        </Button>
         <Button
            android:id="@+id/button2"
            android:text="2"
            android:layout_marginLeft="10dp" 
            android:background="@drawable/willi"> 
         </Button>
         <Button
            android:id="@+id/button3"
            android:text="3" 
            android:layout_marginLeft="10dp"
            android:background="@drawable/willi"> 
          </Button>
          <Button
            android:id="@+id/button4"
            android:text="4" 
            android:layout_marginLeft="10dp"
            android:background="@drawable/willi"> 
           </Button>


    </TableRow>
        <TableRow 
            android:id="@+id/tableRow2"

       android:layout_weight="1"
       >

        <Button
            android:id="@+id/button5"
            android:text="1" 
             android:background="@drawable/willi"> 
            </Button>
                    <Button
                        android:id="@+id/button6"
            android:text="2"
            android:layout_marginLeft="10dp" 
             android:background="@drawable/willi"> 
            </Button>
                    <Button
                        android:id="@+id/button7"
            android:text="3" 
            android:layout_marginLeft="10dp"
             android:background="@drawable/willi"> 
            </Button>
                    <Button
                        android:id="@+id/button8"
            android:text="4" 
            android:layout_marginLeft="10dp"
             android:background="@drawable/willi"> 
            </Button>


    </TableRow>
  and so on....

</TableLayout>

问题是,如果一个按钮处于活动状态,它看起来像这样:

enter image description here

按钮位于彼此的顶部,它们很好奇地伸展开来。

所以,我测试了在开始时设置所有按钮,然后它看起来像这样(它应该看起来如何 - 所有按钮在另一个旁边): enter image description here

我的代码如何更改布局文件?

1 个答案:

答案 0 :(得分:2)

  

我的代码如何更改布局文件?

不是。 resource个文件无法在运行时更改。

正在发生的事情是,您将它们设置为GONE,以便其占用的空间可供其他Button使用。不要将它们设置为GONE,而是尝试制作它们INVISIBLE。这样,空间仍将被占用,但它们不会显示。

只是一个建议但你可以在xml中设置visibility来清理你的Java文件。这只是一个偏好,但这可能就是我要做的。另外,

/*
    some comments
    some other comments
*/

是如何进行阻止评论。