定义最大RelativeLayout大小

时间:2012-10-26 07:57:40

标签: android android-layout relativelayout

我必须在屏幕上定义5个布局,并将它们放置为3x3网格。布局是空的。 enter image description here

在某些时候,我希望以编程方式在一个布局上添加一些视图,并且该视图不得超过分配的布局大小。换句话说,我希望在其中放置任何视图的布局,并且保证它不会在该布局上增长。重要说明:我无权手动更改添加视图的大小,我只能控制其最大大小。

我现在有这样的布局:

<RelativeLayout
    android:id="@+id/pluginsLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"     
    android:layout_above="@+id/mainButtons"
    android:layout_below="@+id/paramsLayout">
        <RelativeLayout
            android:id="@+id/capturingPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"           
            android:layout_gravity="fill_horizontal|left"
            android:layout_above="@+id/processingPluginLayout">                                         
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/processingPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"               
            android:layout_gravity="fill_horizontal|left"
            android:layout_above="@+id/filterPluginLayout">                                     
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/filterPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_weight="1"
            android:layout_gravity="fill_horizontal|left|bottom"                
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true">                                  
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/exportPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"               
            android:layout_gravity="fill_horizontal|right"
            android:layout_above="@+id/viewfinderPluginLayout">                                     
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/viewfinderPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"                                       
            android:layout_gravity="fill_horizontal|bottom|right"
            android:layout_toLeftOf="@+id/filterPluginLayout"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true">                                     
        </RelativeLayout>
    </RelativeLayout>

我在代码中添加了新视图:

public void addVFPluginView(View view)
{       
    ((RelativeLayout)MainScreen.thiz.findViewById(R.id.viewfinderPluginLayout)).addView(view);      
}

我希望得到这样的行为:

enter image description here

但我只得到了这个,查看填充整个父布局:

enter image description here

注意:我使用API​​8(没有GridLayout)

2 个答案:

答案 0 :(得分:0)

你可以在API8中使用网格布局。

答案 1 :(得分:0)

我设法解决了这个问题。 主要思想是以编程方式自己控制视图的大小。 我计算每个区域的适当比例,当有人为这个布局添加一个新视图时,我会检查该视图大小是否适合分配的布局大小。如果大小不合适,我修改此视图的LayoutParams。 这是空布局的xml:

<RelativeLayout
    android:id="@+id/pluginsLayout"             
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"             
    android:layout_above="@+id/mainButtons"
    android:layout_below="@+id/paramsLayout">
        <LinearLayout
            android:id="@+id/capturePluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true">               
        </LinearLayout>
        <LinearLayout
            android:id="@+id/processingPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true">                                           
        </LinearLayout>
        <LinearLayout
            android:id="@+id/filterPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"                
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true">                                            
        </LinearLayout>
        <LinearLayout
            android:id="@+id/exportPluginLayout"
            android:orientation="vertical"              
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true">                                               
        </LinearLayout>
        <RelativeLayout
            android:id="@+id/viewfinderPluginLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"             
            android:layout_height="wrap_content"                                        
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true">                                                        
        </RelativeLayout>
    </RelativeLayout>

就像我那样管理添加视图的大小:

public void addVFPluginView(View view)
{
    //Get maximum width and height for such plugins
    int layoutHeight = this.getViewfinderLayoutHeight();
    int layoutWidth = this.getViewfinderLayoutWidth();

    //Calculate appropriate size of added plugin's view
    android.widget.RelativeLayout.LayoutParams viewLayoutParams = (android.widget.RelativeLayout.LayoutParams)view.getLayoutParams();
    viewLayoutParams = this.getTunedRelativeLayoutParams(view, viewLayoutParams, layoutWidth, layoutHeight);

//Get fixed maximum size for this plugin type
public int getViewfinderLayoutWidth()
{
    return ((RelativeLayout)MainScreen.thiz.findViewById(R.id.pluginsLayout)).getWidth()/2;
}

public int getViewfinderLayoutHeight()
{
    return (int)(((RelativeLayout)MainScreen.thiz.findViewById(R.id.pluginsLayout)).getHeight()*0.7);
}


    ((RelativeLayout)MainScreen.thiz.findViewById(R.id.viewfinderPluginLayout)).addView(view, viewLayoutParams);        
}

android.widget.RelativeLayout.LayoutParams getTunedRelativeLayoutParams(View view, android.widget.RelativeLayout.LayoutParams currParams, int goodWidth, int goodHeight)
{
    int viewHeight, viewWidth;

    if(currParams != null)
    {
        viewHeight = currParams.height;
        viewWidth = currParams.width;


        if(viewHeight > goodHeight || viewHeight <= 0)
            viewHeight = goodHeight;

        if(viewWidth > goodWidth || viewWidth <= 0)
            viewWidth = goodWidth;

        currParams.width = viewWidth;
        currParams.height = viewHeight;

        view.setLayoutParams(currParams);
    }
    else
    {       
        currParams = new android.widget.RelativeLayout.LayoutParams(goodWidth, goodHeight);
        view.setLayoutParams(currParams);
    }

    return currParams;
}

最后,下一个问题适用于具有合适大小的多个视图,但总的来说它们大于分配的大小。也许我只是直接计算视图的大小,如果下一个添加视图的大小的总和将超过分配的大小,我只是不允许添加此视图。