2017-07-06 37 views
0

我在這裏有一個佈局,其中有一些嵌套的佈局,其中包含我想要啓用/禁用的各種視圖。我認爲這將會像調用setEnabled()一樣直接,但我發現只適用於和SwitchesSeekbars,TogglebuttonsImagebuttons不以編程方式對setEnabled()作出反應,也沒有android:enabled=""可用於XML。我在這裏錯過了什麼?某些視圖可以啓用/禁用,而其他視圖不可以

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <!-- ***** Zoom ***** --> 
    <RelativeLayout 
     android:id="@+id/RL_zoom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="1dp" 
     android:background="@drawable/layout_bg_transluscent" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/textview_zoomLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:layout_centerInParent="true" 
      android:text="Zoom" 
      android:textColor="#ff43b7ff" 
      android:textSize="20sp" /> 

     <ImageButton 
      android:id="@+id/imagebutton_zoomOut" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginStart="15dp" 
      android:layout_toEndOf="@+id/textview_zoomLabel" 
      android:background="@null" 
      android:src="@drawable/ic_zoom_out_white_24dp" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_toEndOf="@+id/imagebutton_zoomOut" 
      android:layout_toStartOf="@+id/imagebutton_zoomIn" 
      android:orientation="horizontal"> 

      <SeekBar 
       android:id="@+id/seekbar_zoom" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/textview_zoomValue" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="5dp" 
      android:layout_toStartOf="@+id/textview_zoomPercentageLabel" 
      android:text="50" /> 

     <TextView 
      android:id="@+id/textview_zoomPercentageLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_centerInParent="true" 
      android:text="%" /> 

     <ImageButton 
      android:id="@+id/imagebutton_zoomIn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_toStartOf="@id/textview_zoomValue" 
      android:background="@null" 
      android:src="@drawable/ic_zoom_in_white_24dp" /> 
    </RelativeLayout> 

    <!-- ***** Focus ***** --> 
    <RelativeLayout 
     android:id="@+id/RL_focus" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/RL_zoom" 
     android:layout_marginTop="2dp" 
     android:background="@drawable/layout_bg_transluscent" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/textview_focusLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:layout_below="@+id/togglebutton_autofocus" 
      android:layout_centerInParent="true" 
      android:text="Focus" 
      android:textColor="#ff43b7ff" 
      android:textSize="20sp" /> 

     <ImageButton 
      android:id="@+id/imagebutton_focusOut" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginStart="14dp" 
      android:layout_toEndOf="@+id/textview_focusLabel" 
      android:background="@null" 
      android:src="@drawable/ic_zoom_out_white_24dp" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_toEndOf="@+id/imagebutton_focusOut" 
      android:layout_toStartOf="@+id/imagebutton_focusIn" 
      android:orientation="horizontal"> 

      <SeekBar 
       android:id="@+id/seekbar_focus" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/textview_focusValue" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="5dp" 
      android:layout_toStartOf="@+id/textview_focusPercentageLabel" 
      android:text="50" /> 

     <TextView 
      android:id="@+id/textview_focusPercentageLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_toStartOf="@+id/togglebutton_autofocus" 
      android:text="%" /> 

     <ImageButton 
      android:id="@+id/imagebutton_focusIn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_toStartOf="@id/textview_focusValue" 
      android:background="@null" 
      android:src="@drawable/ic_zoom_in_white_24dp" /> 

     <ToggleButton 
      android:id="@+id/togglebutton_autofocus" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="3dp" 
      android:minHeight="1dp" 
      android:minWidth="1dp" 
      android:textAllCaps="false" 
      android:textColor="#ff43b7ff" 
      android:textOff="AF&#10;Off" 
      android:textOn="AF&#10;On" /> 
    </RelativeLayout> 

    <!-- ***** Iris ***** --> 
    <RelativeLayout 
     android:id="@+id/RL_iris" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/RL_focus" 
     android:layout_marginTop="2dp" 
     android:background="@drawable/layout_bg_transluscent" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/textview_irisLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:layout_below="@+id/togglebutton_autoiris" 
      android:layout_centerInParent="true" 
      android:text="Iris" 
      android:textColor="#ff43b7ff" 
      android:textSize="20sp" /> 

     <ImageButton 
      android:id="@+id/imagebutton_irisOut" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginStart="41dp" 
      android:layout_toEndOf="@+id/textview_irisLabel" 
      android:background="@null" 
      android:src="@drawable/ic_zoom_out_white_24dp" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_toEndOf="@+id/imagebutton_irisOut" 
      android:layout_toStartOf="@+id/imagebutton_irisIn" 
      android:orientation="horizontal"> 

      <SeekBar 
       android:id="@+id/seekbar_iris" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/textview_irisValue" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="5dp" 
      android:layout_toStartOf="@+id/textview_irisPercentageLabel" 
      android:text="50" /> 

     <TextView 
      android:id="@+id/textview_irisPercentageLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_toStartOf="@+id/togglebutton_autoiris" 
      android:text="%" /> 

     <ImageButton 
      android:id="@+id/imagebutton_irisIn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_toStartOf="@id/textview_irisValue" 
      android:background="@null" 
      android:src="@drawable/ic_zoom_in_white_24dp" /> 

     <ToggleButton 
      android:id="@+id/togglebutton_autoiris" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="3dp" 
      android:minHeight="1dp" 
      android:minWidth="1dp" 
      android:textAllCaps="false" 
      android:textColor="#ff43b7ff" 
      android:textOff="AI&#10;Off" 
      android:textOn="AI&#10;On" /> 
    </RelativeLayout> 

    <!-- ***** On/Off ***** --> 
    <RelativeLayout 
     android:id="@+id/RL_device_tools_on_off" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/RL_iris" 
     android:layout_marginTop="2dp" 
     android:background="@drawable/layout_bg_transluscent" 
     android:orientation="horizontal"> 

     <Switch 
      android:id="@+id/switch_device_power_on_off" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="2dp" 
      android:text="Device Power" 
      android:textColor="#ff43b7ff" /> 

     <ImageButton 
      android:id="@+id/imagebutton_device_refresh" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toEndOf="@+id/switch_device_power_on_off" 
      android:src="@drawable/ic_refresh_white_24dp" /> 
    </RelativeLayout> 

</RelativeLayout> 

回答

0

雖然每個視圖有setEnabled方法,the interpretation of the value set through it varies by subclass

在您的情況下,TextViewSwitchToggleButton具有邏輯以在設置時響應此值。其他人不。以類似的方式,您只能對這些屬性設置android:enabled屬性,因爲it is defined as an attribute that can be used with TextViews

如果你想有SeekbarImageButtonsetEnabled值作出響應,您可以創建擴展各自的那些,覆蓋setEnabled方法自定義視圖,然後實現您想如何這些視圖作出反應。

+0

有趣。爲什麼在這些'views'上使用'setEnabled'的人有那麼多的例子呢? – shoota

+0

我糾正了我以前的答案。 'ToggleButton'也應該響應'setEnabled'調用:https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ToggleButton.java#150 您的'ToggleButton'在禁用時是否會發生變化? 我不是很確定你可能會提到哪些例子,但我的猜測是他們可能正在利用'StateListDrawables'。這些允許您根據視圖的狀態顯示不同的drawable(我們的情況正在啓用/禁用)。 – MrAlbean

+0

嗯,事實證明,'setEnabled()'實際上可以在這些'views'上工作。當我在一行中的三個「SeekBars」中的第一個調用'setOnTouchListener()'時發現了,現在每個其他'view'都對'setEnabled()'做出了反應。所以'setOnTouchListener()'有些事情阻止了'setEnabled()'的生效,但我找不到任何有關爲什麼的信息。有什麼想法? – shoota

0

事實證明,我有一些錯誤,我得到了修復。我發佈了我學到的東西,因爲我之前沒有看到它,如果有人遇到這個問題。如果將自定義顏色設置爲TextView,則調用setEnabled()將不會影響顏色以使其看起來被禁用。同樣,對於ImageView,我將背景設置爲null,這也使得按鈕看起來不像setEnabled()。我確信其他Views有這樣的細微差別,我只是還沒有找到他們。

相關問題