Android UI中setLayoutParams方法的问题

我正在制作一个应用程序,我必须更改UI程序。 我需要更改layout_heightlayout_width XML属性。

但是,当我使用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>

任何时候你设置LayoutParams,检查视图的父容器。 在你所有的例子中,父类是一个LinearLayout。 您需要使用LinearLayout.LayoutParams,如下所示:

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

无论这是你的特殊力量问题,我们都无法知道。 正如评论中所述,您应该检查LogCat并在此处发布例外详情。 尽管试试这个,看看它是否解决了这个问题。


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

即使您要在代码中设置宽度和高度,您需要将这些参数添加到每个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"
    />

我再次检查并根据上述答案进行了两项更改。 它仍然没有工作。

但是,我发现我在使用setContentView()方法之前正在执行函数。 由于观点尚未建立,因此无法进行修改。

所以把我的函数放在setContentView()方法之后,它可以正常工作。

不管怎么说,多谢拉。

链接地址: http://www.djcxy.com/p/93281.html

上一篇: Problem with setLayoutParams method in android UI

下一篇: gravity in LinearLayout