我想创建一个按钮,当它被按下时,它会改变大小(稍微小一点),当按钮再次被释放后,它的大小应该会变回正常大小。
我已经找到了一个xml选择器的一个很好的按钮,改变颜色时按下。我试图改变只是填充,你可以看到的xml,但它不工作。
为什么不工作?我该怎么做呢?
我还试图使Button
被触摸时包含的Button
和LinearLayout
失效。
我膨胀的布局:
<?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>
Padding实际上给出了按钮内部指定的空间。所以它不会一直改变按钮的大小。最好为正常状态和按下状态指定不同的宽度和高度
// 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);
试着用程序来做,…
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;
}
});
这是你想要的:-
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"/>
使用 scaleDownX .play(scaleDownX).with(scaleDownY),它将保持动画位置而不会恢复到原始位置
希望这能解决你的问题,谢谢
在你的Button的onClick方法中,你可以改变padding和任何你想用Button方法编程的东西
你不能通过xml选择器改变大小或填充,你可以通过onClick方法上的java代码来做到这一点。
这个适用于调整大小按钮。
但是在我的linearLayout我有2个这样的按钮,每次我按下一个linearLayout计算其他按钮的宽度。这意味着另一个按钮的大小也会发生变化。看起来很奇怪。
我想要的是我有两个按钮彼此相邻,当我按下只是一个应该改变大小当它按下
任何想法?
如何设置padding:
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;
}
});
}
}
}
使用此处描述的android:variablePadding
布尔。如果可绘制对象的填充应该根据所选的当前状态改变,则为"true";如果填充应该保持不变(基于所有状态的最大填充),则为"false"。启用此功能需要在状态更改时处理执行布局,而这通常是不支持的。默认为false