我有一个包含 5 个片段的活动。在其中一个片段中有 aButton
和 a RelativeLayout
。我希望我的按钮能够改变它RelativeLayout
的高度或宽度。
我的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
,如果我将变量设为 final,我通常无法在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)