2013-11-01 133 views
0

我有一個活動,它包含5個片段。在其中一個片段中有ButtonRelativeLayout。我想讓我的按鈕能夠改變這個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) 
+0

如果您進行Sum最終,您仍然可以訪問它自己及其方法/孩子。你只是不能改變對象本身,例如這是行不通的:final Object x = null; x = new Object();因爲它的「最終」聲明爲空。但最終的CustomObject y = new CustomObject(); y.changeProperties(); y.doSomething();仍然有效。 – damian

+0

@damian我做了你說的,但它又崩潰了,我發佈了錯誤日誌,請你檢查一下嗎? –

+0

你可以顯示你的XML佈局嗎? – damian

回答

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

sSummary,哪怕是一個RelativeLayout,是LinearLayout內。視圖的佈局參數指的是父級佈局,因此這裏佈局參數的正確類型將是LinearLayout.LayoutParams。這是導致類拋出異常的原因。

通常一個好方法是修改現有佈局參數,如getLayoutParams()所檢索的那樣。在就地修改返回的參數後,請求使用requestLayout()setLayoutParams()也隱含地這樣做)重新佈局。

+0

這是你第二次「真正」解決我的問題:D非常感謝! –

0

爲什麼你得到一個錯誤的原因是因爲你不能被類外實例化沒有它最終的OnClickListener內引用變量。

事實上,這是一個Fragment不應該改變任何東西,這只是一個正確的問題。您可以簡單地進行以下更新:

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

而且這應該可以解決您收到的錯誤。

+0

我做了你所說的但又崩潰了,我發佈了錯誤日誌,請你檢查一下嗎? –

0

好的,原因是因爲某些原因,問題是LinearLayout。我將其更改爲RelativeLayout,我的問題已修復。這到底是什麼LinearLayout?我信了你。