2013-01-19 27 views
0

嗨我遇到了設置可見性的問題。我的XML文件:Android元素可見性無法正常工作

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    android:id="@+id/widget324" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/onbg" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
> 

<Button android:id="@+id/topBtn" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true"> 
</Button> 

<VideoView 
    android:id="@+id/introvid" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
</VideoView> 
<ImageView 
    android:id="@+id/Kn8s" 
    android:visibility="invisible" 
    android:src="@drawable/kn8s" 
    android:layout_width="430.50dp" 
    android:layout_height="121.00dp" 
    android:layout_gravity="bottom|center_horizontal" 
    android:layout_marginBottom="10.00dp" 
    /> 
<ImageView 

    android:id="@+id/introimage" 
    android:src="@drawable/pcintro" 
    android:layout_width="215.25dp" 
    android:layout_height="197.00dp" 
    android:layout_gravity="center" 
    /> 

</FrameLayout> 

和我的java文件是:

import (...) 

public class Intro extends Activity { 
    VideoView vid; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     overridePendingTransition(R.anim.fadein,R.anim.fadeout); 
     setContentView(R.layout.intro); 
     vid = (VideoView)findViewById(R.id.introvid); 
     String urlpath = "android.resource://" + getPackageName() + "/" + R.raw.onbg; 
vid.setVideoURI(Uri.parse(urlpath)); 
     vid.start(); 
     FrameLayout widget324 = (FrameLayout)findViewById(R.id.widget324); 
     widget324.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         Intent OnIntent = new Intent(Intro.this, On.class); 
         startActivity(OnIntent); 
         Intro.this.finish(); 
       // TODO Auto-generated method stub 
       } 
       return true; 
      }});  

     final MediaPlayer mpGo = MediaPlayer.create(this, R.raw.intro); 
     mpGo.start();   

     final ImageView productions = (ImageView)findViewById(R.id.Kn8s); 
     productions.postDelayed(new Runnable() { 
       public void run() { 
        productions.findViewById(R.id.Kn8s).setVisibility(ImageView.VISIBLE); 
       } 
      }, 10500); 

      new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
        Intent OnIntent = new Intent(Intro.this, On.class); 
        startActivity(OnIntent); 
        Intro.this.finish(); 
      } 
    }, 12000);  
     } } 

問題是,這裏所說的 「(R.id.Kn8s)」 的元素應該10,5秒後apear(10500 )但它沒有。我注意到,當時間設置在1秒(999)以下時,它工作正常!還注意到,當我扔掉視頻視圖時,「Kn8s」元素會在應該是時間之後變爲可見!爲什麼它不起作用?它應該被聲明onStart方法?或者我不知道...是關於軟件加速的問題? THX每一個答案:)

+0

你有沒有得到點 – Janmejoy

回答

2

嘗試更換:

productions.findViewById(R.id.Kn8s).setVisibility(ImageView.VISIBLE); 

productions.setVisibility(View.VISIBLE);

延遲之前,你已經初始化生產,所以您不必再經過初始化延時。

不知道我怎麼錯過了,不過,這似乎是這個問題:

productions.postDelayed(new Runnable() { 
      public void run() { 
       productions.findViewById(R.id.Kn8s).setVisibility(ImageView.VISIBLE); 
      } 
     }, 10500); 

new Handler().postDelayed(new Runnable() { 
      public void run() { 
       productions.setVisibility(View.VISIBLE); 
      } 
     }, 10500); 
+0

是的,謝謝。我已經嘗試過,但沒有解決問題,但是知道我可以用這種方式編碼:)並且代碼看起來更好! – NightKn8

0

嘿感謝替換上面的代碼Janmejoy的答案我已經找到解決方案我的問題!所有我必須做的是改變我的XML的這一部分:

<ImageView 
    android:id="@+id/Kn8s" 
    android:visibility="invisible" 
    android:src="@drawable/kn8s" 
    android:layout_width="430.50dp" 
    android:layout_height="121.00dp" 
    android:layout_gravity="bottom|center_horizontal" 
    android:layout_marginBottom="10.00dp" 
    /> 

到:

<ImageView 
    android:id="@+id/Kn8s" 
    android:visibility="gone" 
    android:src="@drawable/kn8s" 
    android:layout_width="430.50dp" 
    android:layout_height="121.00dp" 
    android:layout_gravity="bottom|center_horizontal" 
    android:layout_marginBottom="10.00dp" 
    /> 

那麼誰能告訴我,爲什麼「水漲船高」,而「隱形」不工作!? acording到: https://stackoverflow.com/a/7348547/1873367 「 View.GONE這種觀點是不可見的,並且它不採取任何空間佈局的目的 View.INVISIBLE這種觀點是無形的,但它仍然佔用空間佈局的目的 。」 所以如果是這樣,在我的情況下它應該不重要!還是應該?

相關問題