2014-05-09 307 views
0

我想創建一個按鈕,當它被按下時會改變大小(稍小一些),並且在按鈕被再次釋放後,大小應該變回正常大小。Android:當按鈕狀態按下時按鈕填充改變

我發現了一個很好的按鈕的xml選擇器,它可以在按下按鈕時改變顏色。 我試圖改變只是填充,你可以看到在XML中,但它不起作用。

它爲什麼不起作用?我怎樣才能做到這一點?

我也嘗試使ButtonLinearLayout無效,Button在觸摸時包含它。

佈局,我膨脹:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    android:weightSum="1" 
    > 

    <Button 
     android:id="@+id/cancel_button" 
     style="@style/ButtonText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="@drawable/purplebutton" 
     android:text="@string/done_button" 
     android:layout_weight ="0.5"/> 

    <Button 
     android:id="@+id/done_button" 
     style="@style/ButtonText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="@drawable/yellowbutton" 
     android:text="@string/done_button" 
     android:layout_weight ="0.5"/> 

</LinearLayout> 

purplebutton.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
     <shape> 
      <solid 
       android:color="#a276eb" /> 
      <stroke 
       android:width="1dp" 
       android:color="#6a3ab2" /> 
      <corners 
       android:radius="3dp" /> 
      <padding 
       android:left="5dp" 
       android:top="5dp" 
       android:right="5dp" 
       android:bottom="5dp" /> 
     </shape> 
    </item> 
    <item> 
     <shape> 
      <gradient 
       android:startColor="#a276eb" 
       android:endColor="#6a3ab2" 
       android:angle="270" /> 
      <stroke 
       android:width="1dp" 
       android:color="#6a3ab2" /> 
      <corners 
       android:radius="4dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 
</selector> 

回答

0

在您的按鈕的方法的onClick,您可以更改填充和你用按鈕編程方法,什麼都想要。

0

您不能通過xml選擇器更改大小或填充,您可以通過onClick方法上的java代碼執行此操作。

1

填充實際上給出了按鈕內的指定空間。所以它不會總是改變按鈕的大小。更好地爲正常狀態和按下狀態指定不同的寬度和高度。

1
// try this way,hope this will help you... 

put "button_selector.xml" under "drawable" folder 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/buttonhoverimage" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/buttonhoverimage" android:state_focused="true"></item> 
    <item android:drawable="@drawable/buttonnormalimage" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item> 
    <item android:drawable="@drawable/buttonhoverimage" android:state_enabled="false"></item> 

</selector> 

try to use button selector this way 
button.setBackgroundResource(R.drawable.button_selector); 
1

嘗試這樣做編程,...

mControls.UpButton.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction() & MotionEvent.ACTION_MASK) { 
       case MotionEvent.ACTION_DOWN: 
         FrameLayout fl = new FrameLayout(context); 
         Button b = new Button(context); 
         FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height); 
         params.setMargins(top, left, bottom, right); 
         fl.addView(b,params); 
        break; 
       case MotionEvent.ACTION_UP: 
         FrameLayout fl = new FrameLayout(context); 
         Button b = new Button(context); 
         FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height); 
         params.setMargins(top, left, bottom, right); 
         fl.addView(b,params); 
        break; 
       } 
       return false; 

      }   
     }); 
0

這一個適用於大小調整按鍵。

但在我的linearLayout中,我有2個這樣的按鈕,每次我按一個linearLayout計算其他按鈕的寬度。這也意味着另一個按鈕可以稍微改變尺寸。看起來很奇怪。

我想要的是,我有兩個相鄰的按鈕,當我按下只是應該改變它的大小時,它的按下。

任何想法?

我這裏怎麼設置填充:

int size1 = btnLayout.getChildCount(); 
for (int i = 0; i < size1; i++) { 
    if (btnLayout.getChildAt(i) instanceof View) { 
      final Button mybutton1; 
      final Button mybutton2; 
      View myView = btnLayout.getChildAt(i); 
      if (myView.getId() == id.cancel_button) { 
       // cancel button 
       mybutton1 = (Button) btnLayout.getChildAt(i); 
       mybutton1.setOnTouchListener(new View.OnTouchListener() { 

        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         int action = event.getAction(); 
         if (action == MotionEvent.ACTION_DOWN) { 
          mybutton1.setPadding(20, 20, 20, 20); 

         } else if (action == MotionEvent.ACTION_UP) { 
          mybutton1.setPadding(10, 10, 10, 10); 
         } 
         return false; 
        } 
       }); 

      } else if (myView.getId() == id.done_button) { 
       // done button 
       mybutton2 = (Button) btnLayout.getChildAt(i); 
       mybutton2.setOnTouchListener(new View.OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         int action = event.getAction(); 

         if (action == MotionEvent.ACTION_DOWN) { 
          mybutton2.setPadding(20, 20, 20, 20); 

         } else if (action == MotionEvent.ACTION_UP) { 
          mybutton2.setPadding(10, 10, 10, 10); 
         } 
         return false; 
        } 

       }); 
      } 
    } 



    } 
0

使用Android:variablePadding as described here

布爾。如果可繪製的填充值應基於所選的當前狀態而改變,則爲「true」如果填充保持不變(基於所有狀態的最大填充),則爲「false」。啓用此功能要求您在狀態更改時處理執行佈局,這通常不受支持。默認爲false。

0

這裏是你想要的東西: -

Your_Button.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(Your_Button, 
          "scaleX", 0.8f); 
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(Your_Button, 
          "scaleY", 0.8f); 
        scaleDownX.setDuration(1000); 
        scaleDownY.setDuration(1000); 

        AnimatorSet scaleDown = new AnimatorSet(); 
        scaleDown.play(scaleDownX).with(scaleDownY); 

        scaleDown.start(); 


        break; 

       case MotionEvent.ACTION_UP: 
        ObjectAnimator scaleDownX2 = ObjectAnimator.ofFloat(
          Your_Button, "scaleX", 1f); 
        ObjectAnimator scaleDownY2 = ObjectAnimator.ofFloat(
          Your_Button, "scaleY", 1f); 
        scaleDownX2.setDuration(1000); 
        scaleDownY2.setDuration(1000); 

        AnimatorSet scaleDown2 = new AnimatorSet(); 
        scaleDown2.play(scaleDownX2).with(scaleDownY2); 

        scaleDown2.start(); 


        break; 
       } 
       return true; 
      } 
     }); 

在這裏你需要在XML中做什麼: -

<Button 
 
     android:id="@+id/done_button" 
 
     style="@style/ButtonText" 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_gravity="center" 
 
     android:scaleX="1" 
 
     android:scaleY="1" 
 
     android:background="@drawable/yellowbutton" 
 
     android:text="@string/done_button" 
 
     android:layout_weight ="0.5"/>

化妝用scaleDown.play的(scaleDownX ).with(scaleDownY),它會保持動畫的位置,不會恢復到原來的

希望它解決您的問題歡呼