我的 ImageButton 不改变状态有问题。当我单击或触摸按钮时,它保持相同的图像。这是我用作选择器的 XML。
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/pushed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/pushed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/pushed" />
<item
android:drawable="@drawable/default" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我从我的 main.xml 中调用这个选择器作为
android:background="@drawable/imagechoice"
Run Code Online (Sandbox Code Playgroud)
imagechoice.xml 是带有选择器的文件
我不明白为什么这不起作用,除非我必须有一些 Java 代码,但我所看到的一切都表明这应该有效。
我对 Android 很陌生。
我创建了一个图像按钮。main.xml 包含如下代码段。
<ImageButton
android:id="@+id/button1"
android:layout_width="50px"
android:layout_height="50px"
android:src="@drawable/cat"
android:layout_x="50px"
android:layout_y="52px"
>
Run Code Online (Sandbox Code Playgroud)
我必须把我拥有的“cat.png”放在哪里。我需要将其重命名为其他名称吗?
我有以下XML:
<ImageButton
android:id="@+id/SwapTextButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="0.5"
android:background="@drawable/my_gradient_button"
android:scaleType="fitCenter"
android:src="@drawable/extended_text" />
Run Code Online (Sandbox Code Playgroud)
它输出一个如下所示的按钮:

有没有办法增加src资源占用的大小?我的意思是将src保持在按钮区域内,但要使它比现在大.
我已经尝试更改scaleType,但任何其他设置都会导致src资源在按钮边界之外显示自身的部分(也就是它变得太大).
我有一项研究,似乎有基于java的解决方案,但理想情况下我也希望将解决方案保留在xml中.
有人可以建议修复/指出我正确的方向吗?
谢谢
我想做以下链接:http://imagizer.imageshack.us/v2/800x600q90/42/gbs4.jpg
但是当我按下 ImageButton 时,我希望将声音设置为铃声。
这是我的代码:
公共类 ListViewAdapter 扩展 BaseAdapter {
私有Context上下文;
私人清单项目;
私有 LayoutInflater 充气器;
字符串路径=“SD卡/媒体/铃声”;
公共ListViewAdapter(上下文上下文,
列出项目 ) {
inflater = LayoutInflater.from( 上下文 );
this.context = 上下文;
this.items = 项目;
}
公共 int getCount() {
返回 items.size();
}
公共对象 getItem(int 位置) {
返回 items.get(位置);
}
公共长 getItemId(int 位置) {
返回位置;
}
公共视图 getView(int 位置,视图 ConvertView,ViewGroup 父级){
字符串 item = items.get(position);
查看 v = null;
if( 转换视图!= null )
v = 转换视图;
别的
v = inflater.inflate( R.layout.list_row, 父级, false); … 如果单击按钮,我想打开 wifi 并更改 ImageButton 的图像,但我不知道如何更改它
我也尝试过如何在每次点击时更改按钮的图像?但不起作用:
boolean isPressed=false
button.setOnClickListener(buttonListener);
OnClickListener buttonListener= new OnClickListener() {
@Override
public void onClick(View v) {
if(isPressed){
button.setBackgroundResource(R.drawable.icon1);
}else{
button.setBackgroundResource(R.drawable.icon2);
}
isPressed=!isPressed;
}};
Run Code Online (Sandbox Code Playgroud)
当我编写上面的代码时,android studio 显示:无法解析符号setOnClickListener
我还创建了一个button_wifi_selector xml,它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:drawable="@drawable/wifi_on"
android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
android:state_focused="true" />
<item android:drawable="@drawable/wifi" />
</selector>
Run Code Online (Sandbox Code Playgroud)
在我的活动中我有这个
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton_wifi"
android:layout_below="@+id/toggleButton_wifi"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="turnOffWifiDemo"
android:src="@drawable/button_wifi_selector" />
Run Code Online (Sandbox Code Playgroud)
但它没有做,我想要什么有人可以帮助我吗?谢谢
编辑:它适用于第一个代码。我只需从 xml 中的 ImageButton 中删除 onClick 但是:当我启动应用程序时,他正在第二次更改图片。之后他每次都会改变
我刚刚将我的 maui android/ios 项目更新为 .net 8,我的 ImageButtons 现在呈现不同的方式。
在 .net 7 中,我没有设置 ImageButton 的高度/宽度,按钮的大小将根据填充进行调整。我找到了一个我喜欢的平衡,它的图像显示有一些填充,这样它就不会被圆角半径切断。
在 .net 8 中,我需要设置高度/宽度才能显示 ImageButton。但是,设置 ImageButton 高度/宽度尺寸后,填充值没有任何区别(这与 .net 7 中的行为相同)。
由于某种原因,FontImageSource 现在渲染得比以前更大,并且填充没有任何区别,我无法让图像很好地适合按钮而不被角半径切断。FontImageSource 图标没有改变。
我尝试了各种不同的高度/宽度、填充和角半径,以及设置 fontImageSource 的大小,但我无法复制 .net 7 中此按钮的外观!
我想让它看起来像上图中顶部中间的图标。有谁知道需要更改什么才能使图标正确显示?
<Grid Margin="0" BackgroundColor="Black" >
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ImageButton Grid.Row="1" Margin="15,0,15,10"
HorizontalOptions="End"
VerticalOptions="End"
HeightRequest="40"
WidthRequest="40"
Padding="5"
CornerRadius="20"
BackgroundColor="#32578f">
<ImageButton.Source>
<FontImageSource
Glyph="{x:Static m:MaterialOutlined.Grid_view}"
FontFamily="MaterialOutlined"
Color="White"
Size="10"/>
</ImageButton.Source>
</ImageButton>
</Gird>
Run Code Online (Sandbox Code Playgroud) 有一个带有提交按钮图像的php表单.我试图确定在页面回发时是否点击了提交按钮.试过
$testForm = 'fail';
if (isset($_POST['btnSubmit'])) {
$testForm = 'Submit clicked';
}
Run Code Online (Sandbox Code Playgroud)
按钮代码:
<input name="btnSubmit" value="Submit" style="float: right;" type="image" src="images/submit.gif" width="181" height="43" alt="Submit Form" />
Run Code Online (Sandbox Code Playgroud)
但它似乎没有起作用.尝试在页面上获取其他输入元素的值,它们工作正常.是否有一些处理图像按钮的特殊方法?
我在programmaticaly中创建了按钮,而不是在xml文件中.然后我想在这个链接中设置LayoutParams:如何以编程方式设置相对布局中按钮的layout_align_parent_right属性?
但是当我试图发射时,我有一个例外.
这是我的代码:
RelativeLayout ll2 = new RelativeLayout(this);
//ll2.setOrientation(LinearLayout.HORIZONTAL);
ImageButton go = new ImageButton(this);
go.setId(cursor.getInt(cursor.getColumnIndex("_id")));
go.setClickable(true);
go.setBackgroundResource(R.drawable.go);
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // LogCat said I have Null Pointer Exception in this line
go.setLayoutParams(params1);
go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i3 = new Intent(RecipesActivity.this, MedicineActivity.class);
i3.putExtra(ShowRecipe, v.getId());
i3.putExtra("Activity", "RecipesActivity");
RecipesActivity.this.startActivity(i3);
}
});
ll2.addView(go);
Run Code Online (Sandbox Code Playgroud)
为什么我的应用会抛出异常?谢谢.
这是我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main"
android:background="@android:color/black" >
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/about"
android:layout_centerHorizontal="true"
android:background="@drawable/calendar" />
<ImageButton
android:id="@+id/okan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/calendar"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/calendar"
android:background="@drawable/okanliyiz"/>
<ImageButton
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp"
android:layout_toLeftOf="@+id/calendar"
android:background="@drawable/info" />
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我写了它的程序化部分和Drawer工作就好了,只是我不能点击它背后的ImageButtons .如果我把按钮放在前面我可以点击它们,但是抽屉被留下了,这是一个可怕的景象.这是什么解决方案?我怎样才能点击抽屉后面的按钮并看到前面的抽屉?
提前致谢.
android imagebutton android-layout navigation-drawer drawerlayout

红色方块是按钮的边界,而图像保持居中的32x32px.我已经尝试使用button.getImage()将位置和大小设置为按钮的值,但似乎没有任何效果.
imagebutton ×10
android ×7
.net-8.0 ×1
button ×1
drawerlayout ×1
forms ×1
java ×1
layoutparams ×1
libgdx ×1
listview ×1
maui ×1
maui-android ×1
php ×1
resize ×1
sdk ×1
selector ×1
src ×1
state ×1
submit ×1