Android自定义布局的孩子没有得到测量的高度和宽度

时间:2012-03-08 11:27:53

标签: android layoutparams

我已经实现了一个由两个片段组成的活动(称为HomeScreenActivity)。其中一个显示项目列表,另一个显示所选项目的详细信息。细节片段(我的代码中的ResumeFragment)由一系列组件组成,用户可以通过将其指定为配置文件中的多个单元格来配置高度和宽度。这些组件将添加到名为ComponentsLayout的自定义布局中。

打开HomeScreenActivity时,我会在onCreate方法中显示每个片段,并按预期显示它们。 但是当我点击项目列表片段中的项目,并且我将resumeFragment替换为同一类的新实例时,不会显示任何内容。

我花了很多时间调试,我看到resumeFragment,确实填写了它给出的高度和宽度,但componentsLayout只有1的高度。这个我发现是因为高度和宽度尽管我在componentsLayout onMeasure方法中设置了layoutParams,但componentLayout中包含的组件没有设置。

以下是HomeScreenActivity,ResumeFragment和ComponentsLayout的代码:

public class HomeScreenActivity extends FragmentActivity implements OnItemSelectedListener {
.
.

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

    setContentView(R.layout.home_screen);

    .
    .

    //Check if device is in landscape orientation 
    if(width > height && isTablet) {
        //The patient context fragment must maintain the same width as when in portrait orientation.
        findViewById(R.id.patient_resume_fragment_container).setLayoutParams(new LinearLayout.LayoutParams(height, height));
        findViewById(R.id.screen_pager).setLayoutParams(new LinearLayout.LayoutParams(width-height, height));
        fragmentManager = getSupportFragmentManager();
        patientResumeFragment = new ResumeFragment();
        testFragment = new NysomTestFragment();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.patient_resume_fragment_container, patientResumeFragment);
        fragmentTransaction.commit();
    }   
}

.
.

@Override
public void onItemSelected(Item item) {
    itemResumeFragment.getView().findViewById(R.id.componentsContainer);
    itemResumeFragment = new ResumeFragment(item);
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.item_resume_fragment_container, itemResumeFragment);
    fragmentTransaction.commit();
}

ResumeFragment

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragmentView = inflater.inflate(R.layout.resume_fragment,container, false);
    ComponentsLayout componentsContainer = (ComponentsLayout) fragmentView.findViewById(R.id.componentsContainer);
    placeComponents(componentsContainer);
    return fragmentView;
}

private void placeComponents(ComponentsLayout layout) {
    List<ComponentConfig> configs = new ArrayList<ComponentConfig>(testMobile.getModel().getComponentConfiguration());

    int viewId = 1;

    for (ComponentConfig currConfig : configs) {
        ComponentsLayout.LayoutParams params = new ComponentsLayout.LayoutParams(0, 0);
        params.setWidthInCells(currConfig.getWidth());
        params.setHeightInCells(currConfig.getHeight());

        if (viewId == 1) {
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        } else {
            if (currConfig.getPositionX() > 0) {
                // looking for rightOf viewId
                params.addRule(RelativeLayout.RIGHT_OF, getRightOfComponentId(configs, currConfig, viewId - 1));
            }
            if (currConfig.getPositionY() > 0) {
                // looking for below viewId
                params.addRule(RelativeLayout.BELOW, getBelowComponentId(configs, currConfig, viewId - 1));
            }
        }

        try {
            // this code will be changed in story 9
            View view = currConfig.getClassType().newInstance().getView(getActivity());
            view.setId(viewId);
            view.setBackgroundColor(colors[(viewId - 1) % 6]);
            layout.addView(view, params);
        } catch (Exception e) {
            // we should never get to this point as configuration is validated
            Log.e(TAG, "getView: unable to create component for position " + viewId, e);
            throw new RuntimeException(e);
        }

        viewId++;
    }
}

和ComponentLayout

public class ComponentsLayout extends RelativeLayout {

public ComponentsLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int horizontalCellsNum = getResources().getInteger(R.integer.resume_horizontal_cells_num);
    int cellHeight = (int) (getResources().getDimension(R.dimen.resume_cell_height));

    int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        LayoutParams params = (LayoutParams) child.getLayoutParams();
        params.width = parentWidth * params.getWidthInCells() / horizontalCellsNum;
        params.height = cellHeight * params.getHeightInCells();
        child.setLayoutParams(params);
    }
}

再一次,当从HomeScreenActivity onCreate方法调用时,这是有效的,但是当从onItemSelected方法完成时则不行。

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我在componentLayout上调用onMeasure之前,通过向组件添加所有布局参数来创建一个解决方法。这不是我想要的解决方案,但它很简单,这可能是我忽视它的原因。