[英]Crash when setting parameter to layout in Android
單擊切換按鈕將顯示菜單並向右滑出內容視圖。 動畫完成后,內容視圖的布局參數會更新到最終位置。
在更新內容視圖的最終位置時,語句mViewContent.setLayoutParams(params);
導致崩潰。 錯誤消息是java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
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
<?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
參數應分別添加到您的父視圖中,假設您的父視圖為LinearLayout
,則必須使用LinearLayout.LayoutParams
。
解釋(改編自這里): -
以LinearLayout.LayoutParams
和RelativeLayout.LayoutParams
為例,它們是不同的獨立類。 它們存儲有關子視圖的不同附加信息……比如說……
LinearLayout.LayoutParams
可以將權重值與每個視圖關聯,而RelativeLayout.LayoutParams
不能。RelativeLayout.LayoutParams
可以具有像alightWithParent
、 above
、 below
這樣的值,而LinearLayout.LayoutParams
不能。 雖然代碼不會給出編譯時錯誤,因為所有LayoutParams
都有相同的父類,即ViewGroup.LayoutParams
。 因此,始終必須根據父布局分配Layout
參數。
確保您導入了正確的布局參數
import android.widget.LinearLayout.LayoutParams;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.