无法在onCreate中设置LayoutParams

时间:2012-01-20 12:20:31

标签: java android eclipse layout

这是一个小型原型,但它重复了真实程序的错误。

文件TestLPActivity:

package cz.gangnus.testlp;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class TestLPActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout allForm = (LinearLayout) findViewById(R.id.AllChannelsListForm);
        int formWidth=1080;
        int formHeight=540;
        allForm.setLayoutParams(new LayoutParams(formWidth,formHeight));
    }
}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/AllChannelsListForm"
    android:layout_width="1080dp"
    android:layout_height="wrap_content"
    android:background="@drawable/channels_list_bg"
    android:gravity="top|left"
    android:orientation="vertical" >
</LinearLayout>

默认清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.gangnus.testlp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TestLPActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

png文件的宽度为1140,高度为570

如果没有设置layoutparams,一切都会好的。但随着应用程序在使用以下堆栈完成onResume之后会下降:

FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 3129 
FrameLayout.onMeasure(int, int) line: 250   
FrameLayout(View).measure(int, int) line: 8313  
LinearLayout.measureVertical(int, int) line: 531    
LinearLayout.onMeasure(int, int) line: 309  
LinearLayout(View).measure(int, int) line: 8313 
PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, int, int, int)     line: 3138   
PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 250    
PhoneWindow$DecorView(View).measure(int, int) line: 8313    
ViewRoot.performTraversals() line: 839  
ViewRoot.handleMessage(Message) line: 1859  
ViewRoot(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 130 
ActivityThread.main(String[]) line: 3683    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not    available [native method]   
Method.invoke(Object, Object...) line: 507  
ZygoteInit$MethodAndArgsCaller.run() line: 839  
ZygoteInit.main(String[]) line: 597 
NativeStart.main(String[]) line: not available [native method]  

将param设置移动到onResume不会改变行为中的任何内容。

事先感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

试试这种方式

LayoutParams params = allForm.getLayoutParams();
params.width=1080;
params.height=540;
allForm.setLayoutParams(params);