FrameLayout布局

相当于相对布局是轻量级的,不管拖进来多少个控件,他们都是重叠的

总的布局也可以有一个id

FragmentLyout   root;

package com.example.learnfragment;


import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;


public class FragmentMainActivity extends Activity {
private FrameLayout root;
private ImageView a;
private ImageView b;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_main);
a=(ImageView) findViewById(R.id.a);
b=(ImageView) findViewById(R.id.b);
root=(FrameLayout) findViewById(R.id.root);
showB();
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
   if(a.getVisibility()==View.VISIBLE){
    showB();
   }else{
    showA();
   }

}
});
}
    public void showA(){
    a.setVisibility(View.VISIBLE);
    b.setVisibility(View.INVISIBLE);
   
    }
    public void showB(){
    b.setVisibility(View.VISIBLE);
    a.setVisibility(View.INVISIBLE);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragment_main, menu);
return true;
}


}



1、FrameLayout布局代码:


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/xia" 
    />
      <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shang" 
        android:layout_gravity="center"
    />

</FrameLayout>

2、布局效果




你可能感兴趣的:(FrameLayout布局)