动态设置相对布局的布局参数

时间:2011-11-27 11:02:12

标签: android android-layout

我需要帮助来动态更改已存在于xml中的布局的布局参数,其中包含layout_width和layout_height的一些预定义值。我想在我的java代码中动态地给出这些新值。我使用以下代码,但我收到错误,不知道如何继续。请帮忙

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

请帮忙!!!!

1 个答案:

答案 0 :(得分: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);