2013-11-25 34 views
0

我有一個應用程序,我可以找到藍牙設備。當我開始搜索時,會出現一個對話窗口,在上方顯示,配對設備及其下方顯示新發現的設備。對話窗口布局上的權重

這個窗口和藍牙聊天的例子是一樣的,我從那裏得到它。

會發生什麼情況是,如果您有幾個配對的設備,新設備的空間會變得非常小。 1

這是該對話窗口中的代碼:我用這個代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TextView android:id="@+id/title_paired_devices" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/title_paired_devices" 
    android:textIsSelectable="true" 
    android:visibility="gone" 
    android:background="#666" 
    android:textColor="#fff" 
    android:paddingLeft="5dp" /> 

<ListView android:id="@+id/paired_devices" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stackFromBottom="true" 
    android:layout_weight="1"/> 

<TextView android:id="@+id/title_new_devices" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/title_other_devices" 
    android:textIsSelectable="true" 
    android:visibility="gone" 
    android:background="#666" 
    android:textColor="#fff" 
    android:paddingLeft="5dp"/> 

<ListView android:id="@+id/new_devices" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stackFromBottom="true" 
    android:layout_weight="2"/> 

<Button android:id="@+id/button_scan" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/button_scan"/> 

+1

根據設備高度將屏幕分爲兩部分,並在運行時設置列表視圖的高度 – Meenal

+0

或者您可以使用可擴展的'ListView'。 –

+0

@Meenal夏爾馬我該怎麼做? – masmic

回答

0

,和我分了我的屏幕在20-80

try { 

      private int imageHeight1, imageHeight2; 
      Display mDisplay = getActivity().getWindowManager() 
        .getDefaultDisplay(); 
      if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 13) { 

       int height = mDisplay.getHeight(); 
       imageHeight1 = (int) (height * 0.2); 
       imageHeight2 = height - imageHeight1; 

      } else { 

       Point size = new Point(); 
       mDisplay.getSize(size); 

       imageHeight1 = (int) (size.y * 0.2); 
       imageHeight2 = size.y - imageHeight1; 
      } 

      try { 
       listview1.getLayoutParams().height = imageHeight1; 
       listview2.getLayoutParams().height = imageHeight2; 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

會有一些方法直接在佈局上做,而不使用代碼? – masmic

0

嘗試的口糧此代碼...

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight=".9" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".5" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/title_paired_devices" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#666" 
      android:paddingLeft="5dp" 
      android:text="title_paired_devices" 
      android:textColor="#fff" 
      android:textIsSelectable="true" 
      /> 

     <ListView 
      android:id="@+id/paired_devices" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:stackFromBottom="true" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".5" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/title_new_devices" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#666" 
      android:paddingLeft="5dp" 
      android:text="title_other_devices" 
      android:textColor="#fff" 
      android:textIsSelectable="true" /> 

     <ListView 
      android:id="@+id/new_devices" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:stackFromBottom="true" /> 
    </LinearLayout> </LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight=".1" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button_scan" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="button_scan" /> </LinearLayout> 

</LinearLayout> 
+0

不會做正確的工作。只顯示半按鈕。新設備從按鈕開始加載。是不是最佳性能的最佳方法 – masmic

+0

我認爲你需要佈局背景上的透明圖像。 –