Wherever I look, FrameLayout
seems to be used as the FragmentContainer
.
Why is FrameLayout
always seen with Fragments
?
问题:
回答1:
You can basically use RelativeLayout
or LinearLayout
it will still work,but the answer of your question is in FrameLayout
's documentation :
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
You can read more about FrameLayout
here : FrameLayout/Android Developers
回答2:
Main purpose of frame layout is to block the area required to fit the largest child view. If you use a Frame Layout as Fragment Container you can ensure that you always have the space available to accommodate the largest fragment layout.
In some cases you may need to have more than 1 fragment on screen simultaneously in that case you should prefer Relative or Linear Layout.
回答3:
Everything is based on user requirements and customization needed.
It is not necessary to use FrameLayout at all.
For example in below code there is no layout taken , and fragment itself only contains single imageview .
public class MyFragment extends Fragment implements OnClickListener{
String TAG="MyFragment";
Context c;
MyFragment(Context con){
c=con;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ImageView v=new ImageView(c);
//other imageview stuff
return v;
}
@Overrides
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG, "onClcik");
}
}