我想在我的应用程序中放置一个星形按钮,就像评级星一样。我已经尝试过使用一星评级栏,但不幸的是它不能用作按钮。
我希望它作为一个按钮工作,如果所选状态的背景是黄色的,那就更好了......有什么建议吗?谢谢。
我想在我的应用程序中放置一个星形按钮,就像评级星一样。我已经尝试过使用一星评级栏,但不幸的是它不能用作按钮。
我希望它作为一个按钮工作,如果所选状态的背景是黄色的,那就更好了......有什么建议吗?谢谢。
当然,使用这样的布局资源:
<ImageButton android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/star"
android:background="#00ffffff"
android:onClick="onToggleStar"/>
您可以将其与这样定义的状态列表可绘制对象配对,该可绘制对象保存为 XML 文件,位于res/drawable/star.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@drawable/button_focused" /> <!-- hovered -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
以下是 Android 自带的图片:
您可以只使用内置的btn_star
statelist drawable。
<ImageButton
android:id="@+id/favorite_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@android:drawable/btn_star" //This one!
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#00ffffff" /> //Needed to remove the button background
我希望我最喜欢的按钮表现得像一个复选框,所以这对我来说效果很好,无需在我的 onClickListener 中添加任何图像切换逻辑。在我的 Java 代码中,我可以使用它myFavoriteToggleButton.isChecked()
来确定所采取的操作。
<CheckBox
android:button="@android:drawable/btn_star"
android:padding="@dimen/activity_horizontal_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/favorite"
android:background="@android:color/transparent"
android:layout_gravity="center"/>
这是一个非常好的创建自定义按钮的教程,从头到尾。如果您想变得非常狡猾,您甚至可以为每个按钮状态创建自己的明星形象。
只需在您的活动中添加以下代码:
final ImageButton ButtonStar = (ImageButton) findViewById(R.id.star);
ButtonStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isEnable){
ButtonStar.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),android.R.drawable.btn_star_big_off));
}else{
ButtonStar.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),android.R.drawable.btn_star_big_on));
}
isEnable = !isEnable;
}
});
注意:define boolean isEnable=false global。这是我的 ImageButton xml 代码:
<ImageButton
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@android:drawable/btn_star" />
怎么StateListDrawable
样Button
?试试吧,伙计:)
http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
恐怕Android没有这样的内置控件。您将需要使用 a Button
(或 an ImageButton
),并将背景(或图像资源)设置为带有 states 的可绘制对象。
您可以尝试将其作为文本视图插入。这是我发现的最简单的方法。
在布局 xml 中,我将其添加为文本视图
<TextView
android:id="@+id/tv_star_fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:onClick="onToggleStar"
android:text="★"
android:textSize="40sp" />
然后在代码中写了一个类似下面的函数。
boolean isfavourite;
public void onToggleStar(View view){
TextView favouriteStar = (TextView) view;
if(!isfavourite){
// if the star is not already selected and you select it
isfavourite = true;
favouriteStar.setTextColor(Color.parseColor("#FFD600"));
}else{
// if the star is already selected and you unselect it
isfavourite = false;
favouriteStar.setTextColor(Color.parseColor("#9E9E9E"));
}
}
可能您需要使用对 a (或 an )selector
的状态有帮助的 a 。参考这个Button
ImageButton