在Android中将参数设置为布局时崩溃 [英] Crash when setting parameter to layout in Android

查看:97
本文介绍了在Android中将参数设置为布局时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击切换按钮将显示菜单并向右滑动内容视图。动画完成后,内容视图的布局参数会更新到最终位置。

Clicking on the toggle button will show up the menu and slide out the content view rightward. After animation is finished, the layout parameters of the content view gets updated to the final position.

更新内容视图的最终位置时,语句 mViewContent.setLayoutParams(params); 会导致崩溃。错误消息是 java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams

When updating the final position of the content view, the statement mViewContent.setLayoutParams(params); causes the crash. The error message is java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams

Main.java>公共类MainActivity扩展Activity {}

Main.java > public class MainActivity extends Activity {}

public void onToggleButtonMenuClicked(View view) {
    // Is the toggle on?
    boolean toggleTurnedOn = ((ToggleButton) view).isChecked();

    if (toggleTurnedOn) { // If the toggle is turned on
        // Show menu
        LinearLayout mViewMenu = (LinearLayout) findViewById(R.id.linear_layout_menu);
        Animation animMenuOn = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_menu_on);
        mViewMenu.startAnimation(animMenuOn);

        LinearLayout mViewContent = (LinearLayout) findViewById(R.id.linear_layout_content);
        Animation animContentOff = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_content_off);
        mViewContent.startAnimation(animContentOff);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(480, 800);
        params.leftMargin = 384;  // Shift 384 pixels from left screen border
        params.rightMargin = -96; // Exceed 96 pixels from right screen border
        mViewContent.setLayoutParams(params); // This statement causes crash!
    } else {
          // Hide menu...
    } // End of toggle events handling

} // End of onToggleButtonMenuClicked()

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="329dp"
    android:layout_height="wrap_content" >

    <!-- The Menu View -->
    <LinearLayout
        android:id="@+id/linear_layout_menu"
        android:layout_width="263dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/table_row_1_search_bar"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:weightSum="10"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/edit_text_search_id"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="7"
                android:hint="@string/edit_text_search_id"
                android:textSize="14sp" />

            <Button
                android:id="@+id/button_search_id"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="3"
                android:text="@string/button_search_id" />            

        </LinearLayout>

        <!-- Other rows in the menu are omitted -->

    </LinearLayout> <!-- End of Menu -->

    <!-- The Content View -->
    <LinearLayout
        android:id="@+id/linear_layout_content"
        android:layout_width="329dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ToggleButton
            android:id="@+id/toggle_button_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onToggleButtonMenuClicked" />

        <TextView
            android:id="@+id/text_content"
            android:layout_width="480dp"
            android:layout_height="wrap_content"
            android:text="@string/text_content" />

        </LinearLayout> <!-- End of Content -->

</FrameLayout> <!-- End of the root linear layout -->


推荐答案

mViewContent params应该与resp一起添加到你的父视图中,假设你的父视图为 LinearLayout 然后, LinearLayout.LayoutParams 必须使用。

mViewContent params should be added with resp to your Parent view, suppose you have parent view as LinearLayout then, LinearLayout.LayoutParams must be used.

解释 : -

Explaination:-

例如, LinearLayout.LayoutParams RelativeLayout.LayoutParams ,它们是不同的独立类。他们存储有关子视图的不同附加信息...说..

Take for example, LinearLayout.LayoutParams and RelativeLayout.LayoutParams, they are different independent classes. They store different additional information about child views... say..


  • LinearLayout.LayoutParams 可以将权重值与每个
    视图相关联,而 RelativeLayout.LayoutParams 则不能。

  • RelativeLayout.LayoutParams 的值可以是 alightWithParent 以上,<$ c $每个视图c>
    ,而 LinearLayout.LayoutParams 不能。

  • LinearLayout.LayoutParams can associate weight value with each view, while RelativeLayout.LayoutParams can't.
  • RelativeLayout.LayoutParams can have values like alightWithParent,above, below with each view while LinearLayout.LayoutParams can't.

虽然代码不会给出编译时错误,因为所有 LayoutParams 都有相同的父类,即 ViewGroup.LayoutParams 。因此,总是必须为父布局分配布局参数..

Although the code will not give compile time error because all LayoutParams have same parent class i.e. ViewGroup.LayoutParams. So, it is always essential, to assign Layout params with respect to parent layout..

这篇关于在Android中将参数设置为布局时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆