将类添加到相对布局

时间:2015-12-13 16:00:58

标签: android xml android-layout

实际上我已经制作了显示可滚动表的TableMainLayout,我正在使用result_activity查看它:

中的结果

         protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new TableMainLayout(this));
        }

现在我想将这个表添加到具有一些文本和按钮的result_activity.xml中,然后该怎么办 -

我的activity_result.xml如下 - :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.example.pranav.tegate.result"
        android:background="#010101"
        android:id="@+id/myResult">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hari Bol"
            android:id="@+id/textView3"
            android:textColor="#ffffff"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/a"
            android:layout_below="@+id/textView3"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="33dp" />


    </RelativeLayout>

我也试过了一些事情 - :

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_result);

            TableMainLayout addTable = new TableMainLayout(this);
            addTable.setId(1);

            relativeLayout = (RelativeLayout)findViewById(R.id.myResult);

            RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();


            relativeParams.addRule(RelativeLayout.BELOW , addTable.getId());
            relativeLayout.setLayoutParams(relativeParams);

            relativeLayout.addView(addTable);
        }

但这不起作用 - 我收到以下错误 引起:

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams      cannot be cast to android.widget.RelativeLayout$LayoutParams

如何做到这一点。

2 个答案:

答案 0 :(得分:0)

LayoutParams对象也指父母的View。在您的情况下,RelativeLayout是根,它会膨胀到android.R.id.content FrameLayout,因此ClassCastExceptionRelativeLayout。如果将RelativeLayout包装到另一个Serializable

中,可能会实现您想要实现的目标

答案 1 :(得分:0)

删除此行:

relativeLayout.setLayoutParams(relativeParams);

并更改此行:

relativeLayout.addView(addTable, relativeParams);

而且,如果我正确理解您要做的事情,您可能还想更改addRule()来电:

relativeParams.addRule(RelativeLayout.ABOVE, R.id.textView3);

视图LayoutParams定义该视图在其父视图中的布局方式。您收到ClassCastException,因为relativeLayout实际上位于FrameLayout - 活动的content ViewGroup - 因此它应该FrameLayout.LayoutParams。要将子视图LayoutParams添加到ViewGroup时指定它,您可以将其作为addView()中的第二个参数传递,如上所示。

相关问题