editText不会从gridLayout填充整个列

时间:2016-02-10 08:02:20

标签: android android-fragments android-gridlayout

我有一个简单的GridLayout,每行包含两个textview和两个editexts。 Gui就像这样gridLayout with 2 editexts and 2 textviews

我希望两个editexts都能从gridLayout填充整个可用空间。

使用以下代码以编程方式添加gridLayout和editexts:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        ScrollView indexDefRootView = (ScrollView) rootView.findViewById(R.id.scrollViewContainer2);

        GridLayout gridLayout = new GridLayout(getActivity());
        gridLayout.setColumnCount(4);
        gridLayout.setRowCount(20);
        int tmpIndex = 0;
        // Drawable drawable = getResources().getDrawable(R.drawable.backtextcom);

        for (int i = 1; i < 20; i++) {

            if (true) {

                TextView tv = new TextView(getActivity());
                tv.setPadding(60, 0, 10, 30);
                Spec row = GridLayout.spec(tmpIndex);
                Spec col = GridLayout.spec(0);
                tv.setText("Left Text " +(i-1));
                GridLayout.LayoutParams first = new GridLayout.LayoutParams(row, col);
                gridLayout.addView(tv, first);

                EditText editText1 = new EditText(getActivity());
                //editText1.setBackground(drawable);
                editText1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);

                GridLayout.LayoutParams param = new GridLayout.LayoutParams();
                param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                param.width = GridLayout.LayoutParams.WRAP_CONTENT;
                param.rowSpec = GridLayout.spec(tmpIndex);
                param.columnSpec = GridLayout.spec(1);
                editText1.setLayoutParams(param);               
                gridLayout.addView(editText1);

                TextView tv1 = new TextView(getActivity());
                tv1.setText("Right Text View "+i);
                tv1.setPadding(60, 0, 10, 30);

                row = GridLayout.spec(tmpIndex);
                col = GridLayout.spec(2);
                first = new GridLayout.LayoutParams(row, col);
                gridLayout.addView(tv1, first);

                EditText editText2 = new EditText(getActivity());
                //editText2.setBackground(drawable);
                editText2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                param = new GridLayout.LayoutParams();              
                param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                param.width = GridLayout.LayoutParams.WRAP_CONTENT;
                param.rowSpec = GridLayout.spec(tmpIndex);
                param.columnSpec = GridLayout.spec(3);
                editText2.setLayoutParams(param);               
                gridLayout.addView(editText2);          
                tmpIndex++;
                i=i+2;
            } 
        }

        indexDefRootView.addView(gridLayout);

        return rootView;
    }

我们将不胜感激。

1 个答案:

答案 0 :(得分:2)

在您的代码中尝试这些  在代码中将宽度更改为match_parent

param.width = GridLayout.LayoutParams.MATCH_PARENT;

使用XML是更好的方法。除非必需,否则请避免创建视图