2017-07-06 107 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有这样的细微差别,我只是还没有找到他们。

相关问题