1

我在我的应用程序中使用主/详细信息流。ItemDetailFragment 膨胀 test_grid.xml。因为我想让视图完全适合片段,所以我在 onViewCreated 中插入了一个 OnGlobalLayoutListener() 来测量我的片段并计算视图的完美尺寸。到目前为止一切都很好,但是来自 R.layout 的 test_grid.xml 被默认视图大小膨胀,然后突然膨胀我计算的大小,这当然不是我计划发生的。

我在 onViewCreated() 中为我的 rootView 和 Gridlayout 尝试了 View.Gone,但无法使其可见。所有这些麻烦只是为了防止以编程方式创建由 42 个单元格组成的整个布局。这似乎是更好的方法,除了我无法通过这个问题。有什么建议么?

test_grid.xml:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_test_grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="6"
    android:orientation="horizontal"
    android:rowCount="7"
    android:visibility="visible"
     >

    <Space
        android:id="@+id/space_6"
        android:layout_width="0dp"
        android:layout_height="0dp" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_gravity="fill"
        android:gravity="center_vertical|center_horizontal"
         />
    <EditText
        android:id="@+id/et_name"
        android:layout_gravity="fill"
        android:gravity="center_vertical|center_horizontal"
        />
... more views...
       <Space
        android:id="@+id/space_35"
        android:layout_width="0dp" />
</GridLayout>

ItemDetailFragment.java:包 com.example.ccc;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.Toast;

public class ItemDetailFragment extends Fragment {

    int fragmentWidth;
    int fragmentHeigth;
    int bestRowHeight;
    int bestSpaceWidth;
    int bestViewWidth;
    GridLayout mGrid;
    int view_show;

    public ItemDetailFragment() {
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.test_grid, container,
                false);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        final View rootView = getActivity().findViewById(R.id.my_test_grid);
        // mGrid = (GridLayout) rootView.findViewById(R.id.my_test_grid);
        // mGrid.setVisibility(View.GONE);

        rootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

            @SuppressLint("NewApi")
        public void onGlobalLayout() {
             rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                 fragmentHeigth = rootView.getHeight();
                 fragmentWidth = rootView.getWidth();
                 bestRowHeight = fragmentHeigth / 7;
             bestSpaceWidth = (fragmentWidth / 100) * 10;
             bestViewWidth = (fragmentWidth / 100) * 20;

                // change views here!!!
                View sp6 = rootView.findViewById(R.id.space_6);
                ((GridLayout) rootView).removeView(sp6);
                View sp35 = rootView.findViewById(R.id.space_35);
                ((GridLayout) rootView).removeView(sp35);
                setRowsHeight(sp6, bestRowHeight);
                setColsWidth(sp35, bestSpaceWidth);
                ((GridLayout) rootView).addView(sp6, 5);
                // mGrid = (GridLayout)
                // rootView.findViewById(R.id.my_test_grid);
                // mGrid.setVisibility(View.VISIBLE);
            }
});

super.onViewCreated(view, savedInstanceState);
// mGrid = (GridLayout) rootView.findViewById(R.id.my_test_grid);
// mGrid.setVisibility(View.VISIBLE);

}

    private void setColsWidth(View v, int width) {
        LayoutParams lp;
        lp = v.getLayoutParams();
        lp.width = width;
        v.setLayoutParams(lp);
    }

    private void setRowsHeight(View v, int height) {
        LayoutParams lp;
        lp = v.getLayoutParams();
        lp.height = height;
        v.setLayoutParams(lp);
    }

    public void setWidthHeight(View v, int width, int height) {
        LayoutParams lp;
        lp = v.getLayoutParams();
        lp.width = width;
        lp.height = height;
        v.setLayoutParams(lp);
    }

}

如您所见,我试图使视图在代码的不同位置可见或消失,但没有一个是可行的解决方案。

4

0 回答 0