开发者

How to size android layout dynamically

I need to size the top-most layout of my application dynamically (programmatically) based on information loaded from a configuration file.

Right now the in main.xml I have

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout   
            android:id="@+id/LinearLayout01" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            xmlns:android="http://schemas.android.com/apk/res/android" 
            android:background="@drawable/background">
    </LinearLayout>

So I am looking for a way to change android:layout_width and layout_height programmatically.

Any help would be appreciated

RM

EDIT:

As per suggestions I have tried the following:

LinearLayout l2 = (LinearLayout)findViewById(R.id.LinearLayout07);
LinearLayout.LayoutParams params =  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT,1);
l2.setLayoutParams开发者_开发知识库(params);

But that gives me the following error when running:

Thread [<3> main] (Suspended (exception ClassCastException))    
FrameLayout.onLayout(boolean, int, int, int, int) line: 288 
FrameLayout(View).layout(int, int, int, int) line: 6133 
....


LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT,1);


layout.setLayoutParams(params);

Where layout is your LinearLayout fetched with findViewById. Parameters for the width and height are fill_parent in my example with weight 1.

Instead of fill_parent options you can of course use the fixed size in pixels, or omit the weight if you don't want it.


I detected a mistake when you try to get your LinearLayout. You write it as LinearLayout07 instead of LinearLayout01.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜