在 Android 中, 針對 View 的顯示與隱藏,
其項目有 :
顯示: viewObj.setVisibility( View.VISIBLE );
隱藏 & 影響版面排版: viewObj.setVisibility( View.GONE );
隱藏 & 不影響版面排版: viewObj.setVisibility( View.INVISIBLE );
範例, 參考如下 :
Layout 範例 :
Layout XML
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是文字" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/buttonVisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="VISIBLE" /> <Button android:id="@+id/buttonInvisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="INVISIBLE" /> <Button android:id="@+id/buttonGone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GONE" /> </LinearLayout> </LinearLayout>
程式範例 :
程式碼
public class TomTest extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView( R.layout.main_test ); Button button1 = (Button)findViewById( R.id.buttonVisible); button1.setOnClickListener( this ); Button button2 = (Button)findViewById( R.id.buttonInvisible); button2.setOnClickListener( this ); Button button3 = (Button)findViewById( R.id.buttonGone); button3.setOnClickListener( this ); } @Override public void onClick(View v) { switch( v.getId() ) { case R.id.buttonVisible: // 顯示文字) findViewById(R.id.text).setVisibility( View.VISIBLE ); setTitle( "VISIBLE" ); break; case R.id.buttonInvisible: // 隱藏文字 (不影響版面)) findViewById(R.id.text).setVisibility( View.INVISIBLE ); setTitle( "INVISIBLE" ); break; case R.id.buttonGone: // 隱藏文字 (影響版面)) findViewById(R.id.text).setVisibility( View.GONE ); setTitle( "GONE" ); break; } } }
範例結果 :


