我的活动恰好有5个片段。在其中一个片段中有Button
和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)
答案 0 :(得分:1)
sSummary.setLayoutParams(new RelativeLayout.LayoutParams(250, 250));
您的sSummary
即使是RelativeLayout
,也在LinearLayout
内。视图的布局参数指的是父布局,因此这里布局参数的正确类型为LinearLayout.LayoutParams
。这就是导致类强制转换异常的原因。
通常,一种好方法是修改使用getLayoutParams()
检索的现有布局参数。在就地修改返回的参数后,请求使用requestLayout()
重新布局(setLayoutParams()
也会隐式执行此操作)。
答案 1 :(得分:0)
您收到错误的原因是您无法引用OnClickListener中的一个变量,该变量在类外部实例化而不是最终的。
它是Fragment
的事实不应该改变任何东西,这只是一个正确的问题。您只需进行以下更新:
final RelativeLayout Sum = (RelativeLayout) myFragmentView.findViewById(R.id.Sum);
这应该可以解决您收到的错误。
答案 2 :(得分:0)
好的,由于某些原因,问题是LinearLayout
。我将其更改为RelativeLayout
并解决了我的问题。到底是什么LinearLayout
?我信任你。