使用垂直堆疊的方式配置每個子元件,也就是後面的子元件疊在前面的子元件上面,所以如果每個子元件一樣大小的話,每次只能看到堆疊在最上面的子元件。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/startBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="開始"/> <Button android:id="@+id/endBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" android:text="結束"/> </FrameLayout>
配置兩個 Button,第二個 Button 預設為隱藏。
public class LayoutMgr extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.findViewById(R.id.startBtn).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
LayoutMgr.this.findViewById(R.id.endBtn).setVisibility(
View.VISIBLE);
}
});
this.findViewById(R.id.endBtn).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
}
});
}
}點擊第一個 Button 會將第二個 Button 的 visiblity 改為 visible,點擊第二個 Button 會再將第二個 Button 的 visiblity 改為 gone。因為每個 Button 大小一樣,所以這邊有點偷懶,如果大小不一樣的話,當第二個 Button 顯現時,必須將第一個 Button 隱藏,否則會有露餡的情況發生。
FrameLayout 的寬高是由 visible 與 invisible 的子元件中最大值決定,並不包括 gone 的子元件,因此若要考慮 gone 的狀況,需執行以下的程式:
((FrameLayout) this.findViewById(R.id.frmLyt)).setMeasureAllChildren(true);invisible 與 gone 同樣是將元件隱藏,差別在於 invisible 保留子元件所佔的空間,而 gone 不保留。


沒有留言:
張貼留言