安卓UI中setLayoutParams方法存在问题

4

我正在制作一个应用程序,在其中我需要以编程方式更改UI。 我需要更改XML属性中的layout_heightlayout_width

但是当我使用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");

}

这是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>

1
错误日志将会告诉你a)具体的错误信息,以及b)导致错误的代码所在行数。请发布这些详细信息。 - Ollie C
要获取logcat日志,请在eclipse中打开DDMS透视图或从命令行运行adb logcat - Cheryl Simon
我想知道你是如何调试你的代码的。 - Octavian Helm
3个回答

11

每当您设置LayoutParams时,请检查View的父容器。在所有情况下,父容器都是LinearLayout。您需要使用LinearLayout.LayoutParams,如下所示:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
lv.setLayoutParams(lp);

无论这是否是您特定的强制关闭问题,我们都无法知道。正如评论中所述,您应该检查LogCat并在此处发布异常详细信息。不过,请尝试一下,并查看是否可以解决问题。


2

看起来你的应用程序崩溃了,因为你没有为ImageButton提供android:layout_width和android:layout_height。

即使你打算在代码中设置宽度和高度,你也需要为每个ImageButton添加这些参数到你的xml文件中:

<ImageButton                
  android:id="@+id/rev"               
  android:src="@drawable/reverseimg"  
  android:background="@null"  
  android:scaleType="fitXY"                                               
  android:layout_height="0dp"
  android:layout_width="0dp"
    />

0

我再次检查了上面的答案并进行了两个更改,但仍然没有起作用。

然而,我发现在使用setContentView()方法之前执行了函数。由于视图尚未创建,因此无法进行更改。

因此,在setContentView()方法之后放置我的函数使其正常工作。

无论如何,还是谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接