0

我有一个包含 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)
4

3 回答 3

1
sSummary.setLayoutParams(new RelativeLayout.LayoutParams(250, 250));

你的sSummary,即使它是 a RelativeLayout,也在 a 里面LinearLayout。视图的布局参数是指布局,因此此处正确的布局参数类型是LinearLayout.LayoutParams. 这就是导致类转换异常的原因。

通常,一个好的方法是只修改现有的布局参数,如使用getLayoutParams(). 就地修改返回的参数后,请求重新布局requestLayout()(setLayoutParams()也隐式执行此操作)。

于 2013-11-01T14:23:59.410 回答
0

您收到错误的原因是因为您无法在 OnClickListener 中引用在类之外实例化的变量,而该变量不是最终的。

事实上,它Fragment不应该改变任何东西,这只是一个正确性的问题。您可以简单地进行以下更新:

final RelativeLayout Sum = (RelativeLayout) myFragmentView.findViewById(R.id.Sum);

这应该可以解决您收到的错误。

于 2013-11-01T13:03:42.317 回答
0

好吧,原来问题是LinearLayout出于某种原因。我将其更改为RelativeLayout并且我的问题已解决。什么鬼LinearLayout?我信了你。

于 2013-11-01T14:12:48.933 回答