在我的Android应用程序中,有一个带有绿色背景的圆角矩形按钮。 我使用.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#B5D397" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
和
android:background="@drawable/rounded_btn"
布局文件中的
但是当我按下按钮时没有显示任何效果(没有变化是颜色)所以我使用
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button view = (Button) v;
view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
按下后按钮的颜色变为深绿色。直到这里一切正常,但问题是在释放后按钮颜色仍然是深绿色。我希望它像按下之前一样。我提到了几个在.xml文件中使用选择器的例子,即
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#c0c0c0"
android:state_selected="true"/>
<item
android:color="#ffffff"
android:state_pressed="true"/>
<item
android:color="#9A9A9A"
android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"/>
</selector>
还需要 android:background="@drawable/btn_state"
但我已经使用了 android:background=@drawable/rounded_btn
那么如何兼顾两种效果
我也尝试使用 OnTouchListener
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// show interest in events resulting from ACTION_DOWN
if(event.getAction()==MotionEvent.ACTION_DOWN) return true;
// don't handle event unless its ACTION_UP so "doSomething()" only runs once.
if(event.getAction()!=MotionEvent.ACTION_UP) return false;
doSomething();
button.setPressed(true);
return true;
}
});
但这会停用我的 OnclickListener()
方法,我不想使用 OnTouchListener()
我知道这很傻,但我是android新手 非常感谢
答案 0 :(得分:3)
您必须为此创建3个xml文件...
2表示Drawable Shapes
,1表示Drawable Selector
见下面的代码..
<强> button_normal.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:left="2dp" android:top="2dp">
<shape>
<corners android:radius="10dp" />
<solid android:color="#22151515" />
</shape>
</item>
<item android:bottom="3dp" android:right="3dp">
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="10dp" />
<padding android:left="10dp"
android:right="10dp"/>
<stroke android:width="3px"
android:color="@color/border_pink" />
</shape>
</item>
</layer-list>
<强> button_selected.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:left="2dp" android:top="2dp">
<shape>
<corners android:radius="10dp" />
<solid android:color="#22151515" />
</shape>
</item>
<item android:bottom="3dp" android:right="3dp">
<shape android:shape="rectangle">
<solid android:color="#55fff000"/>
<corners android:radius="10dp" />
<padding android:left="10dp"
android:right="10dp"/>
<stroke android:width="3px"
android:color="@color/border_pink" />
</shape>
</item>
</layer-list>
<强> button_bg.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_focused="true"
android:drawable="@drawable/button_selected"/>
<item
android:state_pressed="true"
android:drawable="@drawable/button_selected"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
最后......
android:background="@drawable/button_bg"
根据需要更改drawable shapes
的代码..
这可能会对你有所帮助
答案 1 :(得分:3)
制作两个不同形状的xml。一个用绿色,另一个用另一种颜色..
并在selector.xml中使用它们
<item android:drawable="@drawable/rounded_btn_green" android:state_selected="true"/>
<item android:drawable="@drawable/rounded_btn_green" android:state_pressed="true"/>
<item android:drawable="@drawable/rounded_btn_green" android:state_focused="true"/>
<item android:drawable="@drawable/rounded_btn"/>
答案 2 :(得分:1)
按照以下步骤 -
在主xml中定义Button
视图,如下所示 -
<Button
android:id="@+id/search_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector_green"
android:text="Search"
android:textColor="@drawable/button_text_color_green"
/>
在您的drawable文件夹中创建button_selector_green
xml文件,如下所示 -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/rounded_corner_transparent" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@drawable/rounded_corner_green"/>
<!-- default -->
</selector>
在您的drawable文件夹中创建button_text_color_green
xml文件,如下所示 -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#48b28a" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#FFFFFF" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
在您的drawable文件夹中创建rounded_corner_transparent
xml文件,如下所示 -
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@android:color/transparent" >
</solid>
<!-- view border color and width -->
<stroke
android:width="1dp"
android:color="#2b8c68" ></stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
<!-- Here is the corner radius -->
<corners android:radius="10dp" >
</corners>
</shape>
在您的drawable文件夹中创建rounded_corner_green
xml文件,如下所示 -
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#48b28a" >
</solid>
<!-- view border color and width -->
<stroke
android:width="1dp"
android:color="#2b8c68" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
<!-- Here is the corner radius -->
<corners
android:radius="10dp" >
</corners>
</shape>
希望这对你有用。快乐编码:)
答案 3 :(得分:0)
Adding a click-effect on a custom button with padded corners
请参阅上述链接中的答案。
我们可以通过在drawable文件夹中只添加一个文件来实现它,并将其作为按钮的背景。
希望这会对某人有所帮助。当出现性能问题时,可以使用我的解决方案并减少对象数量。