2015-11-13 86 views
1

我嘗試將PlotView(自定義視圖)添加到LinearLayout中。我的LinearLayoutweightSum有8個。在我的.xml裏面,我定義了一個Space(3的權重),它必須出現在我的PlotViewButton(權重爲1)之上,應該跟隨我的情節。到現在爲止還挺好。 現在PlotView以編程方式添加,權重爲4.但是,它總是會消耗幾乎整個屏幕,並且我不確定我在這裏做錯了什麼。佈局中的加權視圖

我的代碼:

  1. main_activity.xml(片段)

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="fill_parent" 
        android:layout_alignParentEnd="true" 
        android:id="@+id/linlayout" 
        android:weightSum="8"> 
    
        <Space 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:layout_weight="3"/> 
    
        <Button 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/calibrate" 
         android:id="@+id/calibration" 
         android:layout_weight="1" 
         android:layout_gravity="center_horizontal" /> 
    </LinearLayout> 
    
  2. main_activity.java(片段)

    LinearLayout layout = (LinearLayout) findViewById(R.id.linlayout); 
    plotView.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT, 4f)); 
    layout.addView(plotView, 1); 
    

任何想法我做錯了什麼?

回答

0

的解決方案是高度,這是由重量的影響,設置爲0。

代碼在.xml

android:layout_height="0dp" 

代碼在.java

plotView.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT, 0, 4f));