2015-11-13 68 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));