使用layout_weight有两个LinearLayouts具有相同的高度,不起作用

时间:2015-12-08 00:35:51

标签: android xml android-layout

我很难将这两个LinearLayouts嵌套在一个LinearLayout中以具有相同的高度。第一个LinearLayout的高度为0,而第二个占据整个屏幕。

不确定它是否重要,但我以编程方式用按钮填充第二个LinearLayout。

XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yako.mimibot.pages.RemoteCtrlFragment">

    <LinearLayout
        android:id="@+id/remote_ctrl_ll"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">

        <LinearLayout
            android:id="@+id/terminal_ll"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@drawable/terminal_window">
            <ScrollView
                android:id="@+id/terminal_scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <RelativeLayout
                    android:id="@+id/terminal_rl"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </RelativeLayout>
            </ScrollView>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/remote_gesture_btns_ll"
            android:gravity="center">
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

填充Second LinLay的代码(R.id.remote_gesture_btns_ll)

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_remote_ctrl, container, false);

        mRemoteGestureBtnsLL = (LinearLayout) view.findViewById(R.id.remote_gesture_btns_ll);
        mTerminalRL = (RelativeLayout) view.findViewById(R.id.terminal_rl);

        String[] mimiGestures = getActivity().getResources().getStringArray(R.array.mimi_capable_gestures_array);

        LinearLayout mimiBtnsLL = null;
        Button mimiBtn;
        for (int i=0; i < mimiGestures.length; i++) {
            if (i%2 == 0) {
                mimiBtnsLL = new LinearLayout(getActivity());
                mimiBtnsLL.setOrientation(LinearLayout.HORIZONTAL);
                mimiBtnsLL.setGravity(Gravity.CENTER_HORIZONTAL);
                mimiBtnsLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }

            mimiBtn = new Button(getActivity());
            mimiBtn.setText(mimiGestures[i]);
            mimiBtn.setHeight(100);
            mimiBtn.setWidth(200);
            mimiBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            mimiBtnsLL.addView(mimiBtn);

            if (i%2 == 1) {
                mRemoteGestureBtnsLL.addView(mimiBtnsLL);
            }
        }

        return view;
    }

2 个答案:

答案 0 :(得分:0)

你正在设定重量&amp;您的第一个嵌套LinearLayout显式高度。尝试在Root LinearLayout上显式设置高度,并对嵌套的LinearLayouts使用ONLY layout_weights

答案 1 :(得分:0)

只需将父级LinearLayout(ID为remote_ctrl_ll)的高度设置为match_parent