【发布时间】: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