Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布局位置在Android中未更新 预期结果_Java_Android_User Interface - Fatal编程技术网

Java 布局位置在Android中未更新 预期结果

Java 布局位置在Android中未更新 预期结果,java,android,user-interface,Java,Android,User Interface,与Android上的Facebook应用程序一样,点击屏幕左上角的切换按钮,它将显示菜单并向右滑出内容视图,从而让位于左窗格菜单。滑动动画完成后,内容视图的布局参数将更新到右侧的最终位置,以便切换按钮可以响应对新位置的单击:右上角。然后单击右侧的切换按钮,内容视图将向左滑动并再次隐藏左窗格菜单 问题 更新内容视图的最终位置后,左上角仍然可以响应单击事件,而不是右上角 源代码 Main.java>公共类MainActivity扩展活动{} public void onToggleButtonMen

与Android上的Facebook应用程序一样,点击屏幕左上角的切换按钮,它将显示菜单并向右滑出内容视图,从而让位于左窗格菜单。滑动动画完成后,内容视图的布局参数将更新到右侧的最终位置,以便切换按钮可以响应对新位置的单击:右上角。然后单击右侧的切换按钮,内容视图将向左滑动并再次隐藏左窗格菜单

问题 更新内容视图的最终位置后,左上角仍然可以响应单击事件,而不是右上角

源代码
Main.java>公共类MainActivity扩展活动{}

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()
<?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 -->
活动\u main.xml

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()
<?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 -->

在了解了一些之后,我找到了一个解决方法,类似于

res>anim>anim\u view\u move\u rightword.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >

    <translate
        android:duration="800"
        android:fromXDelta="0%"
        android:toXDelta="80%"
        android:fillEnabled="true"
        android:fillBefore="false"
        android:fillAfter="false"
        android:startOffset="0" />

</set>