在RelativeLayout中动态添加自定义视图

时间:2013-02-21 13:35:28

标签: android android-layout android-custom-view

我正在尝试将自定义视图从xml添加到ScrollView内的RelativeLayout。 自定义视图包含几个TextView。我设置TextViews的文本并设置视图的宽度,高度和topMargin并将其添加到relativeLayout。但我一直在收到错误,我不知道为什么。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout board = (RelativeLayout) findViewById(R.id.Dashboard);
        List<Data> dummyData = new InitData().getDummyData();

        for (Data data : dummyData) {
            View view = null;
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            view = inflater.inflate(R.layout.my_view, board);
            TextView titleTextView = (TextView) view
                    .findViewById(R.id.title_text_view);
            titleTextView.setText(data.getTitle()); //I think error occurs here

            int width = LayoutParams.MATCH_PARENT;
            int height = data.getHeight();
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    width, height);
            params.topMargin = data.getTopMargin();
            view.setLayoutParams(params);
        }
    }

LogCat错误

02-21 20:33:56.903: E/AndroidRuntime(3322): FATAL EXCEPTION: main
02-21 20:33:56.903: E/AndroidRuntime(3322): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:654)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:989)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.ScrollView.onMeasure(ScrollView.java:286)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.os.Looper.loop(Looper.java:123)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at java.lang.reflect.Method.invoke(Method.java:521)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at dalvik.system.NativeStart.main(Native Method)

R.layout.my_view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/data_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

看起来你正在使用错误的LayoutParams。请注意,LayoutParams必须与您将孩子添加到的父视图相匹配。所以如果你有例如一个LinearLayout并希望向其添加RelativeLayout,您必须使用LinearLayout.LayoutParams RelativeLayout.LayoutParams