【问题标题】:Have two LinearLayouts have equal height using layout_weight, not working使用 layout_weight 让两个 LinearLayouts 具有相等的高度,但不起作用
【发布时间】:2015-12-08 10:32:58
【问题描述】:

我很难让嵌套在单个 LinearLayout 中的这两个 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;
    }

【问题讨论】:

  • 你能发布你的代码来填充第二个线性布局吗?您是否尝试将根 layout_height 更改为 match_parent?
  • 刚刚尝试设置高度以匹配父级。没有帮助。还上传了人口代码。
  • 为什么要将 terminal_rl 转换为 RelativeLayout?
  • 我已经在线性布局中添加了一些东西,我会更新 XML。
  • 尝试将父级LinearLayout(id为remote_ctrl_ll )的高度设置为match_parent

标签: android xml android-layout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您正在第一个嵌套的 LinearLayout 上明确设置重量和高度。尝试在您的 Root LinearLayout 上显式设置高度,并仅将 layout_weights 用于嵌套的 LinearLayouts

    【讨论】:

    • 我明确设置高度,因为这是我可以看到内容的唯一方式。最好我不想明确设置任何高度,我希望它们根据屏幕的大小进行拆分。你能发布修复的xml吗?
    • 你是什么意思明确设置高度是查看内容的唯一方法?
    • 正如我没有给它一个除MATCH_PARENTWRAP_CONTENT之外的值,即300dp。顺便说一下,我更新了我的问题中的 XML。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多