我希望layout B
位于布局A之上。我想加入或合并这两个布局。在下图中,圆形绿色和蓝色视图共享相同的布局,但它们被创建为不同的片段。 RoundedColourFragment
类扩展SherlockFragment
并负责自定义片段layout
的大小和形状。我的问题是我无法将two_fragment
布局放在twoFragments
布局的顶部。我一直在onCreateView
RoundedColourFragment
加入他们。这就像覆盖布局一样。
leftFrag = new RoundedColourFragment(getActivity().getResources().getColor(
R.color.trans), 1f, MARGIN, MARGIN / 2, MARGIN, MARGIN);
rightFrag = new RoundedColourFragment(getActivity().getResources().getColor(
R.color.honeycombish_blue), 2f, MARGIN / 2, MARGIN, MARGIN,
MARGIN);
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.add(R.id.twoFragments, leftFrag, "left");
ft.add(R.id.twoFragments, rightFrag, "right");
ft.addToBackStack(null);
ft.commit();
twoFragments.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/twoFragments">
</LinearLayout>
RoundedColourFragment.java
public class RoundedColourFragment extends SherlockFragment {
private View mView;
private int mColour;
private float mWeight;
private int marginLeft, marginRight, marginTop, marginBottom;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mView = new View(getActivity());
GradientDrawable background = (GradientDrawable) getResources()
.getDrawable(R.drawable.rounded_rect);
background.setColor(mColour);
mView.setBackgroundDrawable(background);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, mWeight);
lp.setMargins(marginLeft, marginTop, marginRight, marginBottom);
mView.setLayoutParams(lp);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = vi .inflate(R.layout.two_fragment, container, false);
((ViewGroup) rootView).addView(mView, 0, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
return rootView;
}
}
two_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/two_fragment" >
<Button
android:id="@+id/bbtnn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>