簡體   English   中英

使用layout_weight使兩個LinearLayout具有相同的高度,不起作用

[英]Have two LinearLayouts have equal height using layout_weight, not working

我很難將這兩個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>

填充第二個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;
    }

您正在第一個嵌套的LinearLayout上顯式設置權重和高度。 嘗試在您的Root LinearLayout上顯式設置高度,然后對嵌套的LinearLayouts僅使用layout_weights

只需將父級LinearLayout的高度(標識為remote_ctrl_ll )設置為match_parent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM