【问题标题】:making imagebutton changed when presses按下时更改图像按钮
【发布时间】:2012-09-16 10:20:19
【问题描述】:

我想制作图像按钮,所以当它按下时,我会将未按下的图像替换为按钮的按下图像,然后转到其他活动。我在drawable中有两个不同的图像。

我有两个 xml 文件:

第一个-加载主要活动:背景和图像按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@android:color/black"
 android:orientation="vertical" >



<ImageButton
    android:id="@+id/b1_l"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/s1"
    android:scaleType="centerCrop"  
    android:background="@color/trans"

    android:src="@drawable/b1_l" />

</RelativeLayout>

第二个:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="false"
   android:drawable="@drawable/image_not_pressed" />

   <item android:state_pressed="true"
   android:drawable="@drawable/image_pressed"" />

</selector>

除了我的xml,我还需要写什么代码? 还是我可以传递 xml 并将其仅作为代码编写...?

谢谢!

【问题讨论】:

    标签: android xml android-layout android-button


    【解决方案1】:

    您通常需要具有可绘制的按下状态和默认(未按下)状态。为此,您可以拥有像这样(如下)的 xml 文件。您需要将此xml文件放在drawable文件夹中,然后用作ImageButton的src或Button的背景源。

    我们将其命名为 mybutton.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item android:state_focused="true" android:state_pressed="true" 
                    android:drawable="@drawable/your image name" /> 
        <item android:state_focused="false" android:state_pressed="true" 
                    android:drawable="@drawable/your image name" /> 
        <item   android:drawable="@drawable/your image name" /> 
    </selector> 
    

    主要活动的布局文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@android:color/black"
     android:orientation="vertical" >
    
    
    
    <ImageButton
        android:id="@+id/b1_l"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/s1"
        android:scaleType="centerCrop"  
        android:background="@color/trans"
    
        android:src="@drawable/mybutton" />
    
    </RelativeLayout>
    

    【讨论】:

    • 所以如果我理解正确,图像按钮有三种状态(在你的例子中)?正如你所说,这是一个 imagebutton 的例子。所以在主要活动的xml(imagebutton在哪里)我需要将imagebutton的背景设置为另一个xml文件?机器人:背景“@drawable/***.xml”?谢谢。
    • 是的,还有一种状态叫做专注。在链接中,您可以找到有关选择器可能出现的不同状态的详细信息。但它们都与按钮无关。 developer.android.com/guide/topics/resources/…
    • 在你的主要活动中,布局文件有图像按钮。您需要将 Imagebuttons 源设置为 @drawable/。顺便说一句,您在引用它时不需要 .xml。
    • 我已经修改了答案。如果你觉得有帮助。我建议你标记它。
    【解决方案2】:

    您可能想查看有关此内容的 android 开发人员指南

    http://developer.android.com/guide/topics/ui/controls/button.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-06
      • 2011-01-11
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多