Android中的星形按钮
我想在我的应用程序中放一个星形按钮,就像评级明星一样。我已经尝试过使用一星评级栏,但不幸的是,它不能作为一个按钮工作Android中的星形按钮,android,android-button,Android,Android Button,我想在我的应用程序中放一个星形按钮,就像评级明星一样。我已经尝试过使用一星评级栏,但不幸的是,它不能作为一个按钮工作 我希望它能像一个按钮一样工作,如果所选州的背景是黄色的,那就更好了……有什么建议吗?谢谢。对于按钮,状态列表可绘制如何?试试看,伙计:) 恐怕Android没有这样的内置控件。您需要使用按钮(或ImageButton),并将背景(或图像资源)设置为。当然,请使用如下布局资源: <ImageButton android:id="@+id/favorite" a
我希望它能像一个按钮一样工作,如果所选州的背景是黄色的,那就更好了……有什么建议吗?谢谢。对于
按钮
,状态列表可绘制如何?试试看,伙计:)
恐怕Android没有这样的内置控件。您需要使用
按钮(或ImageButton
),并将背景(或图像资源)设置为。当然,请使用如下布局资源:
<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"/>
以下是Android自带的图片:
- 关
- 在
- 残废
- 迫不及待
- 迫不及待
这是一个很好的教程,从开始到结束都可以创建自定义按钮。你甚至可以为每个按钮状态创建自己的星星图像,如果你想变得非常狡猾的话
可能需要使用选择器,该选择器有助于按钮(或图像按钮
)的状态。请参阅您可以使用内置的btn_-star
状态列表可绘制
<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" />
您可以尝试将其作为文本视图插入。这是我发现的最简单的方法
在布局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" />
我如何才能以更好的分辨率获取这些图像?我直接从\platforms\android-14\data\res\drawable hdpi文件夹中获取了我的图像。文件以我得到的名称“btn_star”开头,无法解析可绘制按钮。。我怎样才能解决这个问题?非常感谢@jkschneider我尝试了你的答案,但当我点击按钮并记录到未定义视图时,它崩溃了。因此,我遵循@Mahyar Salamat关于在java中添加视图的回答。但它仍在破坏应用程序。有什么解决办法吗?我在Fragment中使用这些。请参阅规范的可绘制列表。如何使其变为黄色?
<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"));
}
}