2016-11-22 46 views
0

我有一個使用xml的工作自定義操作欄。我想動態更改由於位置而導致的左側圖像。該位置等於switch語句的情況,所以這不是問題。我做錯了什麼?動態更改自定義操作欄上的ImageView源

ImageView universityLogo; 
    Global global = new Global(); 
    View v2 = View.inflate(this, R.layout.my_action_bar, null); 
    universityLogo = (ImageView)v2.findViewById(R.id.buttonLeft); 
    switch(global.getLocation()){ 
     case "33613": 
      universityLogo.setImageResource(R.drawable.diploma); 
    } 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayShowHomeEnabled(false); 
    View mActionBarView = getLayoutInflater().inflate(R.layout.my_action_bar, null); 
    actionBar.setCustomView(mActionBarView); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
} 

和XML

<ImageButton 
    android:id="@+id/buttonRight" 
    android:layout_marginTop="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_height="30dp" 
    android:layout_width="30dp" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/location" 
    /> 
<ImageView 
    android:id="@+id/buttonLeft" 
    android:layout_marginTop="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_height="30dp" 
    android:layout_width="30dp" 
    android:layout_alignParentLeft="true" 
    /> 
<ImageView 
    android:id="@+id/logo" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:src="@drawable/actionbarlogo" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

回答

2

爲什麼您使用以下行兩次?

View mActionBarView = getLayoutInflater().inflate(R.layout.my_action_bar, null); 

您設置了視圖並再次初始化。你的代碼應該像下面

mageView universityLogo; 
    Global global = new Global(); 
    View v2 = View.inflate(this, R.layout.my_action_bar, null); 
    universityLogo = (ImageView)v2.findViewById(R.id.buttonLeft); 
    switch(global.getLocation()){ 
     case "33613": 
      universityLogo.setImageResource(R.drawable.diploma); 
    } 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayShowHomeEnabled(false); 

    actionBar.setCustomView(mActionBarView); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
+0

誠實的錯誤,謝謝 –

+0

投票通過那些小於15的信譽記錄投,但不改變公開展示後得分 –

+0

沒問題!!!! –