如何在一段时间内扩展一个布局然后另一个布局?

时间:2012-03-13 13:57:52

标签: android layout

我有一个单一的布局,我用它来显示两个不同的实例。请参阅下面的代码,我想这个逻辑应该可行,但它不起作用。

setContentView(R.layout.notification);
//Notify user if Battery level is low
        if (iBatlevel <= 50)
        {

            ImageButton CamButton = (ImageButton) findViewById(R.id.imageButton2);
            CamButton.setVisibility(View.GONE);
            ImageButton PButton = (ImageButton) findViewById(R.id.imageButton1);
            PButton.setVisibility(View.GONE);
            ProgressBar _batPB = (ProgressBar) findViewById(R.id.progressBar);
            _batPB.setProgress(iBatlevel);
            _batPB.setVisibility(View.VISIBLE);
            TextView _batText = (TextView) findViewById(R.id.notifymsg);
            _batText.setText("!!! Low Battery !!!\n"+Integer.toString(iBatlevel)+"%"+" remaining");

            //Wait for 4 seconds
            Handler handler = new Handler(); 
            handler.postDelayed(new Runnable() { 
                public void run() {                    
                } 
            }, 400000); 
        }   
        /*****************************************************************************************/


        //To check whether SDcard is present are not
        boolean isSDCardPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    //    Intent intent = null;
        if(isSDCardPresent)
        {
        //  setContentView(R.layout.sdcard);
            ImageButton CamButton = (ImageButton) findViewById(R.id.imageButton2);
            CamButton.setVisibility(View.VISIBLE);
            ImageButton PButton = (ImageButton) findViewById(R.id.imageButton1);
            PButton.setVisibility(View.VISIBLE);

            ProgressBar _batPB = (ProgressBar) findViewById(R.id.progressBar);
            _batPB.setVisibility(View.GONE);
            Drawable warning_img = getApplicationContext().getResources().getDrawable( R.drawable.warning );
            TextView warning = (TextView) findViewById(R.id.notifymsg);
            warning.setText("SDcard not found !!!");
            warning_img.setBounds( 0, 0, 30, 30 );
            warning.setCompoundDrawables( warning_img, null, null, null );

            // intent = new Intent(this,SDcard.class);
            // startActivity(intent);
           // finish();
        }

如果电池电量低,相应的布局应该显示一段时间然后显示另一个,但是虽然电池回路是真的,但我只得到SD卡视图。知道怎么回事吗?

1 个答案:

答案 0 :(得分:0)

我通常如何处理这个问题是将两个实例扩展为具有公共父布局的单独布局。然后以编程方式切换我想要显示的布局的可见性,具体取决于电池状态(或其他任何内容)。一个布局总是可见,而另一个布局总是GONE。