![](/img/trans.png)
[英]How to make any view grow till I have pressed the button in android.( kotlin or java doesn't matter )?
[英]Android. Button doesn't look pressed
我剛剛開始使用Android開發。 我有一個非常簡單的按鈕,如下所示:
<Button
android:id="@+id/howItWorksButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="58dp"
android:text="@string/how_it_works_button_title" />
在代碼中我為它設置了背景顏色:
howItWorksButton.setBackgroundColor(Color.RED);
這是onClickListener:
howItWorksButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent(MainMenuActivity.this, HowItWorksActivity.class);
MainMenuActivity.this.startActivity(intent);
}
});
問題是,當我點擊它時,它看起來並沒有被按下,它只是保持原樣,沒有變化。 它的onClick方法工作正常,一切都很好用的功能,但我希望它看起來有所改變,就像所有按鈕按下時一樣。
如果您手動設置其顏色,Android不會像iOS那樣自動顯示按下狀態。 這是推薦的方法。
將colors.xml文件夾放在values文件夾中。 它的內容應該是這樣的。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffffff</color>
<color name="darkwhite">#ffeeeeee</color>
<color name="transparent">#00ffffff</color>
<color name="semitransparent">#88000000</color>
<color name="orange">#ffff8800</color>
<color name="light_orange">#ffe27d18</color>
<color name="light_blue">#ff00a2e8</color>
<color name="black">#ff000000</color>
<color name="menuButtonColor">#ffea8e44</color>
</resources>
例如,將選擇器文件放在drawable中
my_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/light_orange" android:state_pressed="true"/>
<item android:drawable="@color/orange"/>
</selector>
像這樣使用您的選擇器
<Button
android:id="@+id/howItWorksButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="58dp"
android:text="how_it_works_button_title"
android:background="@drawable/my_button_selector" />
嘗試這個,
使用Selector
。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/login_btn" android:state_pressed="false"/>
<item android:drawable="@drawable/login_btn_focus" android:state_pressed="true"/>
</selector>
您需要將圖像選擇器設置為背景,以便在按下,聚焦,禁用按鈕時告訴Android您想要顯示哪個圖像或顏色...在這里您可以找到一個示例http://www.mkyong.com/機器人/機器人-ImageButton的選擇器,例如/
在pressed
state
和其他state
寫android:state_pressed=" "
。
按下按鈕時
<item android:state_pressed="true">
<shape>
<solid android:color="@color/green" />
<corners android:radius="@dimen/btn_corner_rds" />
</shape>
</item>
沒有按下按鈕時
<item android:state_pressed="false">
<shape>
<solid android:color="@android:color/transparent" />
<stroke android:width="@dimen/stroke_width_btn" />
<corners android:radius="@dimen/btn_corner_rds" />
</shape>
</item>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.