gpt4 book ai didi

android - 如何在 relativelayout 中以编程方式添加 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:55:05 26 4
gpt4 key购买 nike

我设计了一个RelativeLayout。我正在尝试向其中添加 fragment 。它重叠。我需要做什么才能使其低于其他。

    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 的 View 也会重叠。

<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"/>

最佳答案

我们可以这样做:

存在相对布局的 Activity 类。让该相对布局 (mParentLayout) 成为 Activity View 层次结构的根元素。并且要添加到相对布局的 fragment 将是按钮下方位置的 mGridFragment (mGridBtn)。

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;
}
}

要在 Activity 布局中定位的相应 Fragment 类。

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;
}

}

关于android - 如何在 relativelayout 中以编程方式添加 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13196682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com