【问题标题】:Problem with setLayoutParams method in android UIandroid UI中setLayoutParams方法的问题
【发布时间】:2011-02-09 18:28:05
【问题描述】:

我正在制作一个必须以编程方式更改 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>

【问题讨论】:

  • 错误日志会告诉你 a) 一个特定的错误,b) 你的代码的哪一行导致了错误。请发布这些详细信息。
  • 要获取 logcat 日志,请在 eclipse 中打开 DDMS 透视图或从命令行运行 adb logcat
  • 我想知道你是如何调试代码的。

标签: android android-layout


【解决方案1】:

任何时候设置 LayoutParams,检查视图的父容器。在您的所有实例中,父级都是 LinearLayout。你需要使用LinearLayout.LayoutParams,像这样:

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

这是否是您的特定强制关闭问题,我们无法知道。如 cmets 中所述,您应该检查 LogCat 并在此处发布异常详细信息。试试这个,看看能不能解决问题。

【讨论】:

    【解决方案2】:

    看起来您的应用程序崩溃了,因为您没有为 ImageButtons 提供 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"
        />
    

    【讨论】:

      【解决方案3】:

      我再次检查并按照上述答案中的说明进行了更改。 还是不行。

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

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

      还是谢谢。

      【讨论】:

        猜你喜欢
        • 2011-08-02
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多