2011-11-27 96 views
0

我需要幫助來動態更改xml中已存在的佈局的佈局參數,並使用layout_width和layout_height的一些預定義值。我想在我的java代碼中動態地給這些新的值。我使用下面的代碼,但我得到一個錯誤,不知道如何繼續。請幫忙動態設置相對佈局的佈局參數

RelativeLayout layout = (RelativeLayout)findViewById(R.id.CompLayout); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(10,10); 
layout.setLayoutParams(params); 

請幫忙!!!!

+0

什麼是你的錯誤? –

+0

強制關閉錯誤....沒有異常,因爲我有處理所有異常... – Saiesh

+1

如果你得到一個強制關閉,然後拋出異常。無論您如何處理它,它都會顯示在LogCat中。請在此發佈,我們可以提供幫助。 – Guillaume

回答

0

XML是定義爲

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 

     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <RelativeLayout 
      android:id="@+id/RelativeLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#ffffff"></RelativeLayout> 


    </RelativeLayout> 

和代碼

 RelativeLayout layout = (RelativeLayout) findViewById(R.id.RelativeLayout); 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200, 50); 
      layout.setLayoutParams(params); 
+0

您確定這是有效的,因爲在我的情況下,相對佈局是在一個線性佈局內,但其他方面都是一樣的,但它似乎不工作...... – Saiesh