2015-12-08 45 views
1

我很難讓這兩個LinearLayouts嵌套在一個LinearLayout中具有相同的高度。第一個LinearLayout有0個高度,第二個佔據整個屏幕。

不知道它是否重要,但我用編程方式用按鈕填充第二個LinearLayout。有兩個LinearLayouts使用layout_weight具有相同的高度,不起作用

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; 
    } 
+0

你可以發佈你的代碼來填充第二個線性佈局嗎?並且您是否嘗試將根layout_height更改爲match_parent? –

+0

試圖設置高度來匹配父母。沒有幫助。還上傳了人口代碼。 –

+0

爲什麼要將terminal_rl轉換爲RelativeLayout? –

回答

0

只需設置父LinearLayout(ID爲remote_ctrl_ll)的高度match_parent

0

你對你的第一個嵌套的LinearLayout明確設置權重&高度。試試在你的根LinearLayout中明確設置的高度,並只使用layout_weights的嵌套LinearLayouts

+0

我明確設置高度,因爲它是我能看到內容的唯一途徑。最好我不想明確設置任何高度,我希望它們根據屏幕大小進行分割。你可以發佈修補程序的XML嗎? –

+0

你是指明確設定高度是看內容的唯一方法嗎? – 3alto

+0

正如我沒有給它一個值'MATCH_PARENT'或'WRAP_CONTENT',即'300dp'以外的值。順便提一下,我在我的問題中更新了XML。 –

相關問題