2017-04-16 35 views
0

我有一個按鈕小部件,出現在多個佈局。最終在最終版本中,該按鈕對用戶是隱藏的,但在此之前,我需要它可以在開發的alpha和beta階段看到。如何使用資源值來控制視圖的可見性?

有沒有辦法從一個獨特的資源值控制該按鈕的可見性?

事情是這樣的:

<!-- Feedback button --> 
<Button 
    android:id="@+id/feedback" 
    style="@android:style/Widget.Button.Small" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="@dimen/buttons_margin" 
    android:layout_marginLeft="@dimen/buttons_margin" 
    android:text="Feedback" 
    android:visibility="<some resource value>" 
    /> 

另一種辦法是有一個布爾值,並以編程設置按鈕的知名度,但它必須在每一個活動做這個按鈕,在它的佈局。

回答

1

是的,這是可能的:

android:visibility="@integer/show_view" 

而在你integers.xml -file:

<integer name="show_view">0</string> 

可能的值是

0 (VISIBLE) 
1 (INVISIBLE) 
2 (GONE) 
+0

嗯......你確定字串資源這裏是正確的方法嗎?通常,這些都需要是整數資源,數字值是符號名稱的基礎。 – CommonsWare

+0

我剛剛測試過它,它似乎工作(至少在佈局編輯器中)。使用各個常量的值創建一個int資源也可能會起作用。 – Miraduro

+1

我得到'java.lang.NumberFormatException:Invalid int:'gone''在'setContentView(R.layout.my_layout)'行;' – rraallvv

相關問題