0

我想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();

两个片段.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>
4

0 回答 0