android UI中setLayoutParams方法的问题

SK8*_*K89 4 android android-layout

我正在制作一个应用程序,我必须在其中更改UI programmaticaly.我需要更改layout_heightlayout_widthXML属性.

但是,当我使用该setLayoutParams方法时,应用程序运行,然后我发出一条消息,表示它意外停止工作,我必须强制关闭.

我也尝试过设置LayoutParams ViewGroup.LayoutParams.但没有任何作用.请查看附加的代码和指南.

这是Java代码.

private void fixLayoutVmain(){
    ListView lv;
    ImageButton rev,stop,play,forw;
    lv=(ListView)findViewById(R.id.ListMain);
    rev=(ImageButton)findViewById(R.id.rev);
    stop=(ImageButton)findViewById(R.id.stop);
    play=(ImageButton)findViewById(R.id.plpa);
    forw=(ImageButton)findViewById(R.id.forw);
    Log.d("DEV1","ID's have been assigned");
    LayoutParams lp=new LayoutParams(W, ((75*H)/100));      
    Log.d("DEV1","param object created");       
    lv.setLayoutParams(lp);     
    Log.d("DEV1","ListView param set");     
    lp.height=(int)(10*H)/100;
    lp.width=(int)(10*H)/100;
    Log.d("DEV1","Changes to param made");
    rev.setLayoutParams(lp);    
    Log.d("DEV1","Reverse Button param applied");
    stop.setLayoutParams(lp);
    Log.d("DEV1","Stop button param applied");
    play.setLayoutParams(lp);
    Log.d("DEV1","forward button param applied");
    forw.setLayoutParams(lp);
    Log.d("DEV1","All imagebutton changes have been made");

}
Run Code Online (Sandbox Code Playgroud)

这是XML文件

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListVieW
        android:id="@+id/ListMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="bottom">
            <ImageButton
                android:id="@+id/rev"
                android:src="@drawable/reverseimg"
                android:background="@null"
                android:scaleType="fitXY" />
            <ImageButton
                android:id="@+id/stop"
                android:src="@drawable/stopimg"
                android:background="@null"
                android:scaleType="fitXY" />
            <ImageButton
                android:id="@+id/plpa"
                android:src="@drawable/playimg"
                android:background="@null"
                android:scaleType="fitXY" />
            <ImageButton
                android:id="@+id/forw"
                android:src="@drawable/forwardimg"
                android:background="@null"
                android:scaleType="fitXY" />
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

kco*_*ock 10

每次设置LayoutParams时,请检查View的父容器.在所有实例中,父级是LinearLayout.您需要使用LinearLayout.LayoutParams,如下所示:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
lv.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)

无论这是你的特殊力量关闭问题,我们无从知晓.如评论中所述,您应该检查LogCat并在此处发布异常详细信息.试试这个,看看它是否解决了这个问题.