小编SK8*_*K89的帖子

android UI中setLayoutParams方法的问题

我正在制作一个应用程序,我必须在其中更改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 …
Run Code Online (Sandbox Code Playgroud)

android android-layout

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-layout ×1