我在悬停时看到了许多 ImageButton 示例,但它们更改了图像。我只想改变背景颜色。
这是我的选择器 -
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#4B88F7"/>
<item android:state_pressed="false" android:color="#121212"/>
Run Code Online (Sandbox Code Playgroud)
还有我的 ImageButton -
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@color/actionbar_btn"
android:src="@drawable/ic_remove" />
Run Code Online (Sandbox Code Playgroud) 我想有一个ImageButton是最初有一个灰色的效果就可以了&轮流到其选择时原来的颜色。我正在努力实现类似的目标
. 我知道这可以在 CSS 中完成,但想知道是否存在与它等效的任何 Android 属性/函数。
我在这里读了一篇文章,似乎暗示我必须维护同一图像的不同灰色副本才能实现这一目标。但是,如果我有 100 个以上的 ImageButton,那么维护同一个 Image 的 2 个副本会消耗大量内存。
与常规 Button 不同,ImageButton 或具有图像背景的 Button 在禁用时不会变灰。您实际上必须使用另一个图像或以它看起来变灰的方式对其进行处理。
那么真的没有办法在 Android 中实现在 CSS 中使用灰度可以实现的目标吗?
感谢您的时间!!
我正在尝试创建图像按钮的方法。单击它时,背景音乐停止,图像按钮更改为另一个图像。再次按下时,它会像第一次一样返回并重播音乐。
我正在尝试一个布尔值。当它为真时,音乐开始,当它为假时,音乐开始,但它不起作用!
另外,我如何根据主要活动让另一个活动播放或停止音乐?
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
ImageButton SoundButton;
ImageButton NoSoundButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SoundButton = new ImageButton(this);
NoSoundButton = new ImageButton(this);
/*---------Image Buttons--------*/
SoundButton=(ImageButton) findViewById(R.id.sound);
SoundButton.setVisibility(View.GONE);
NoSoundButton=(ImageButton) findViewById(R.id.nosound);
NoSoundButton.setVisibility(View.VISIBLE);
/*---------Media Player--------*/
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.aud);
mp.setLooping(true);
mp.start();
}
public void nosound(View view) {
SoundButton.setVisibility(View.VISIBLE);
NoSoundButton.setVisibility(View.INVISIBLE);
mp.stop();
mp.prepareAsync();
}
public void sound(View view) {
SoundButton.setVisibility(View.INVISIBLE);
NoSoundButton.setVisibility(View.VISIBLE);
mp.start();
}
}
Run Code Online (Sandbox Code Playgroud) audio android onclick android-mediaplayer android-imagebutton
我想与图像一起使用
<com.gc.materialdesign.views.ButtonFlat
android:id="@+id/buttonflat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E88E5"
android:text="Button" />
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
ImageButton plusButton = new ImageButton(context);
plusButton.setImageResource(x);
Run Code Online (Sandbox Code Playgroud)
我需要一些资源来代替x上面的代码行,例如,实现下图中电话号码字段附近显示的减号和加号按钮之类的功能:
但我不知道这些图片的资源在哪里。所以请帮我解决这个问题。谢谢。
我需要一些帮助,请不要在我的图标或图像下面或下面显示文字,ImageButton我试图使用TextView但是它弄乱我的按钮我不知道如何显示文本.这是我的xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#eeeeee" android:layout_marginTop="30dp">
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="30dp"
android:scaleType="fitCenter"
android:background="@drawable/button1_style"
android:src="@drawable/icon1_48dp"
style="@style/Widget.AppCompat.Button.Colored" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="30dp"
android:scaleType="fitCenter"
style="@style/Widget.AppCompat.Button.Colored"
android:background="@drawable/button2_style"
android:src="@drawable/icon2_48dp"
android:tint="#ffffff"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#eeeeee" android:layout_marginBottom="20dp">
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="30dp"
android:scaleType="fitCenter"
style="@style/Widget.AppCompat.Button.Colored"
android:src="@drawable/icon3_48dp"
android:background="@drawable/button3_style"
android:tint="#ffffff"
/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp" …Run Code Online (Sandbox Code Playgroud) 我有一个ImageButton,我想在我的应用程序的某个Fragment类中使用它.但是当我在我的片段类中使用它时,应用程序崩溃了.但我可以在我的MainActivity中使用它而不会崩溃.我怎样才能解决这个问题?
这是我在我的片段类中使用的代码
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(activity, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.song_popup, popup.getMenu());
popup.show();
}
Run Code Online (Sandbox Code Playgroud)
这会导致应用程序崩溃.但是当我在MainActivity中使用它时(在mainactivity中,"activity"被this.getApplicationContext替换)它可以工作.
我的按钮xml
<ImageButton
android:id="@+id/popUp"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:background="@null"
android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
android:onClick="showPopup" />
Run Code Online (Sandbox Code Playgroud)
logcat的
java.lang.IllegalStateException: Could not find a method showPopup(View) in the activity class com.fm.etunes.phone.MainActivity for onClick handler on view class android.widget.ImageButton with id 'popUp'
at android.view.View$1.onClick(View.java:3814)
at android.view.View.performClick(View.java:4442)
at android.view.View$PerformClick.run(View.java:18473)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at …Run Code Online (Sandbox Code Playgroud)