问题在Android的UI setLayoutParams方法 [英] Problem with setLayoutParams method in android UI

查看:118
本文介绍了问题在Android的UI setLayoutParams方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做我不得不programmaticaly更改UI的应用程序。
我需要改变 layout_height layout_width XML属性。

但是,当我使用 setLayoutParams 方法中,应用程序运行,然后我给的消息,它已意外停止工作,我必须强制关闭。

我已经尝试设置的LayoutParams为 ViewGroup.LayoutParams 为好。
但没有任何工程。请检查连接code和指导。

这是Java code。

 私人无效fixLayoutVmain(){
    LV的ListView;
    的ImageButton转,停止,播放,FORW;
    LV =(ListView控件)findViewById(R.id.ListMain);
    REV =(的ImageButton)findViewById(R.id.rev);
    停止=(的ImageButton)findViewById(R.id.stop);
    玩=(的ImageButton)findViewById(R.id.plpa);
    FORW =(的ImageButton)findViewById(R.id.forw);
    Log.d(DEV1,ID的已分配);
    的LayoutParams LP =新的LayoutParams(W,((75 * H)/ 100));
    Log.d(DEV1,参数对象创建);
    lv.setLayoutParams(LP);
    Log.d(DEV1,ListView控件参数集);
    lp.height =(INT)(10 * H)/ 100;
    lp.width =(INT)(10 * H)/ 100;
    Log.d(DEV1,更改param所做出);
    rev.setLayoutParams(LP);
    Log.d(DEV1,反转按钮参数应用);
    stop.setLayoutParams(LP);
    Log.d(DEV1,停止按钮参数应用);
    play.setLayoutParams(LP);
    Log.d(DEV1,前进按钮参数应用);
    forw.setLayoutParams(LP);
    Log.d(DEV1,所有的ImageButton已经作了修改);}

这是XML文件

 <的LinearLayout
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < LISTVIEW
        机器人:ID =@ + ID / ListMain
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
        < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:方向=横向
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:重力=底>
            <的ImageButton
                机器人:ID =@ + ID /转
                机器人:SRC =@绘制/ reverseimg
                机器人:背景=@空
                机器人:scaleType =fitXY/>
            <的ImageButton
                机器人:ID =@ + ID /停止
                机器人:SRC =@绘制/ stopimg
                机器人:背景=@空
                机器人:scaleType =fitXY/>
            <的ImageButton
                机器人:ID =@ + ID / PLPA
                机器人:SRC =@绘制/ playimg
                机器人:背景=@空
                机器人:scaleType =fitXY/>
            <的ImageButton
                机器人:ID =@ + ID / FORW
                机器人:SRC =@绘制/ forwardimg
                机器人:背景=@空
                机器人:scaleType =fitXY/>
    < / LinearLayout中>
< / LinearLayout中>


解决方案

您设定的LayoutParams任何时候,检查视图的父容器。在所有的情况下,父母是一个LinearLayout中。您需要使用LinearLayout.LayoutParams,就像这样:

  LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(宽,高);
lv.setLayoutParams(LP);

这是否是您的特定强制关闭的问题与否,我们都无从得知。正如评论所说,你应该检查LogCat中并张贴在这里异常的详细信息。试试这个,虽然,看它是否解决了这个问题。

I am making an application in which I have to change the UI programmaticaly. I needed to change the layout_height and layout_width XML attributes.

But when I use the setLayoutParams method, the app runs and then I give a message that it has unexpectedly stopped working and I have to force close.

I have tried setting the LayoutParams as ViewGroup.LayoutParams as well. But nothing works. Kindly check the attached code and guide.

This is the Java Code.

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");

}

This is the XML File

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

解决方案

Any time you set LayoutParams, check the parent container of the View. In all your instances, the parent is a LinearLayout. You need to use LinearLayout.LayoutParams, like so:

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

Whether that's your particular force close issue or not, we have no way of knowing. As stated in the comments, you should check LogCat and post the exception details here. Give this a try though, and see if it fixes the problem.

这篇关于问题在Android的UI setLayoutParams方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆