我有一個活動,它包含5個片段。在其中一個片段中有Button
和RelativeLayout
。我想讓我的按鈕能夠改變這個RelativeLayout
的高度或寬度。使用按鈕的onClick方法更改片段中的佈局參數
我的xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical" >
<Button
android:id="@+id/sSumBut"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:onClick="onClick" >
</Button>
<RelativeLayout
android:id="@+id/sSum"
android:layout_width="250dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:background="@android:color/black" >
</RelativeLayout>
</LinearLayout>
</ScrollView>
正如你所看到的RelativeLayout
的高度爲0。我希望按鈕可以將它更改爲100dp。
這是我Fragment
類:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.up, container, false);
Button sButton = (Button) myFragmentView.findViewById(R.id.sSumBut);
final RelativeLayout sSummary = (RelativeLayout) myFragmentView.findViewById(R.id.sSum);
sButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// When button is clicked.
sSummary.setLayoutParams(new RelativeLayout.LayoutParams(250, 250));
}
});
但是這給了錯誤Cannot refer to a non-final variable
,如果我把這些變量最後,我通常不能動態地改變它的onClick
方法內。請考慮這個課程是Fragment
,我該如何解決這個問題?
錯誤日誌:
11-01 15:07:03.035: E/AndroidRuntime(3048): FATAL EXCEPTION: main
11-01 15:07:03.035: E/AndroidRuntime(3048): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.LinearLayout.measureVertical(LinearLayout.java:360)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1284)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.ScrollView.onMeasure(ScrollView.java:354)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.View.measure(View.java:8366)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.os.Looper.loop(Looper.java:130)
11-01 15:07:03.035: E/AndroidRuntime(3048): at android.app.ActivityThread.main(ActivityThread.java:3693)
11-01 15:07:03.035: E/AndroidRuntime(3048): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 15:07:03.035: E/AndroidRuntime(3048): at java.lang.reflect.Method.invoke(Method.java:507)
11-01 15:07:03.035: E/AndroidRuntime(3048): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-01 15:07:03.035: E/AndroidRuntime(3048): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-01 15:07:03.035: E/AndroidRuntime(3048): at dalvik.system.NativeStart.main(Native Method)
如果您進行Sum最終,您仍然可以訪問它自己及其方法/孩子。你只是不能改變對象本身,例如這是行不通的:final Object x = null; x = new Object();因爲它的「最終」聲明爲空。但最終的CustomObject y = new CustomObject(); y.changeProperties(); y.doSomething();仍然有效。 – damian
@damian我做了你說的,但它又崩潰了,我發佈了錯誤日誌,請你檢查一下嗎? –
你可以顯示你的XML佈局嗎? – damian