如何在relativelayout中以编程方式添加片段

时间:2012-11-02 13:57:21

标签: android android-layout android-fragments relativelayout

我设计了一个RelativeLayout。我正在尝试添加片段。它重叠。我需要做什么才能让它低于另一个。

    FragmentTransaction t = getSupportFragmentManager().beginTransaction();

    FragmentTest myFragment = new FragmentTest();
    t.add(layout.getId(), myFragment, "myFirstFragment");

    FragmentTest myFragment2 = new FragmentTest();
    t.add(layout.getId(), myFragment2, "mySecondFragment");

    t.commit();

// myFragment和myFragment2的视图重叠,即使我已将方向设置为垂直。

<RelativeLayout
    android:id="@+id/idll1"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="10sp"
    android:layout_marginRight="0sp"
    android:layout_marginTop="10sp"
    android:layout_weight="1"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" 
    android:orientation="vertical"/>

4 个答案:

答案 0 :(得分:4)

我们可以这样做:

存在相对布局的活动类。让相对布局(mParentLayout)成为活动视图层次结构的根元素。 并且要添加到相对布局的片段将是位于Button(mGridBtn)下方位置的mGridFragment。

public class DroidOpsActivity extends FragmentActivity implements
    OnClickListener {

    private RelativeLayout mParentLayout = null;
    private Button mGridBtn = null;
    private FragmentManager mFragmentMgr = null;
    private FragmentTransaction mFragmentTransc = null;
    private Fragment mGridFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize();
    }

    private void initialize() {
        setContentView(R.layout.activity_droid_ops);
        mParentLayout = (RelativeLayout) findViewById(R.id.parent_layout);
        mGridBtn = (Button) findViewById(R.id.grid_button);
        mFragmentMgr = getSupportFragmentManager();
        registerListener();
    }

    private void registerListener() {
        mGridBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.grid_button:
                    loadFragment();
                    mGridBtn.setEnabled(false);
        break;
        }
    }

    private void loadFragment() {
        mGridFragment = new DroidOpsFragments();
        mFragmentTransc = mFragmentMgr.beginTransaction();
        mFragmentTransc.add(mParentLayout.getId(), mGridFragment);
        mFragmentTransc.addToBackStack(null);
        mFragmentTransc.commit();
    }

    public RelativeLayout.LayoutParams fetchLayoutParams() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        // We can add any rule available for RelativeLayout and hence can position accordingly
        params.addRule(RelativeLayout.BELOW, mGridBtn.getId());
        return params;
    }
}

相应的片段类,将在活动的布局中定位。

public class DroidOpsFragments extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_word_grid, null);

        // Here we are fetching the layoutParams from parent activity and setting it to the fragment's view.

        view.setLayoutParams(((DroidOpsActivity) activity).fetchLayoutParams());
        return view;
    }

}

答案 1 :(得分:1)

您必须在Fragment的视图中设置布局prarams。

是XML还是hardCode?

如果它们是硬编码,则必须使用向后兼容的支持库获取Google在所有片段视图上放置的FrameLayout Wrapper,并在视图的onlayout或onMeasure中设置其relativelayout参数。

否则,如果它们是XML,只需设置相对规则。

答案 2 :(得分:0)

我认为它应该是LinearLayout

答案 3 :(得分:0)

快速和肮脏似乎有点好。如果你已经知道了你的布局,那么你可以节省一些步骤,而且我知道一个巨大的if(X instanceof Class)块是不受欢迎的,它是非常通用的。

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ViewGroup.LayoutParams p = view.getLayoutParams();
        if (p instanceof CoordinatorLayout.LayoutParams) {
            CoordinatorLayout.LayoutParams params = ((CoordinatorLayout.LayoutParams)p);
            params.gravity = Gravity.END | Gravity.BOTTOM;
            view.setLayoutParams(params);
        }
        else if (p instanceof RelativeLayout.LayoutParams) {
            RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)p);
            params.addRule(RelativeLayout.ALIGN_BOTTOM);
            view.setLayoutParams(params);
        }
}