访问子布局的layoutparams时出现ClassCastException

时间:2013-12-17 10:29:21

标签: android android-layout android-xml

请在以下链接中找到我的启动图片: http://postimg.org/image/yvivajxov/

我一直在尝试在我的活动中创建一个基于触摸手势的滚动条,重新调整大小(高度) 根据手势的布局。如果它是MotionEvent.ACTION_DOWN,它将增加第一个布局的高度并减少第二个布局的高度。

最初,布局在屏幕上均匀分布。以下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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ScrollAct" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <LinearLayout
        android:id="@+id/llout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/red_bar"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1" 
            android:background="@color/red" >
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/green_bar"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:background="@color/green" >
        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

以下是Globals:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scroll);
    linearLayout=(LinearLayout)findViewById(R.id.llout);
    redPanel = (RelativeLayout)findViewById(R.id.red_bar);
    greenPanel = (RelativeLayout)findViewById(R.id.green_bar);

我在活动上的代码如下:

linearLayout.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();

        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) redPanel.getLayoutParams();
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) greenPanel.getLayoutParams();
                _yDelta =Y - lParams.bottomMargin;
                layoutParams.topMargin -=_yDelta;
                lParams.bottomMargin +=_yDelta;
                redPanel.setLayoutParams(lParams);
                greenPanel.setLayoutParams(layoutParams);
                Log.i("Relative LayoutParams",""+redPanel.getLayoutParams().height);

                break;
            case MotionEvent.ACTION_UP:

            case MotionEvent.ACTION_POINTER_DOWN:
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_MOVE:

        }
        return false;
    }});
    isPanelShown=false;

}

并抛出以下异常: -

12-17 08:48:44.633: E/AndroidRuntime(29031): FATAL EXCEPTION: main
12-17 08:48:44.633: E/AndroidRuntime(29031): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
12-17 08:48:44.633: E/AndroidRuntime(29031):    at com.example.multifrag.ScrollAct$1.onTouch(ScrollAct.java:54)
12-17 08:48:44.633: E/AndroidRuntime(29031):    at android.view.View.dispatchTouchEvent(View.java:5624)
12-17 08:48:44.633: E/AndroidRuntime(29031):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1964)
12-17 08:48:44.633: E/AndroidRuntime(29031):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1725)
12-17 08:48:44.633: E/AndroidRuntime(29031):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)

我经历了以下问题提供的解决方案: ClassCastException LinearLayout LayoutParams

并创建了一个tablelayout来访问reqd RelativeLayouts作为父布局,但是它会抛出相同的异常。

1 个答案:

答案 0 :(得分:0)

您必须使用父视图的LayoutParams。如果您在LinearLayout内嵌入RelativeLayout,则必须使用RelativeLayout.LayoutParams