我的主导航菜单有四个按钮,这四个按钮都包含一个图像。 我创建了以下xml文件来处理click事件动画:

是profile.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="false" android:drawable="@drawable/profileicon" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:drawable="@drawable/profileicon" />

并在下面的xml文件中设置效果:

gradient.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/profileicon" />
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
            <gradient
                android:angle="90"
                android:centerColor="#880d0d0f"
                android:endColor="#885d5d5e"
                android:startColor="#880f0f10" />
        </shape>
    </item>
</layer-list>

有什么办法可以避免创建一个新的文件pr。 我有按钮吗?

这就是我的main_layout的样子:

    <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"
    android:background="@drawable/background"
    tools:context=".Main" >

    <Button
        android:id="@+id/btn_yourPreferences"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_alignBottom="@+id/btn_signUp"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="275dp"
        android:background="@drawable/profile"
        android:onClick="onClickViewPreferences" />

    <Button
        android:id="@+id/btn_yourCompetitions"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_above="@+id/btn_yourPreferences"
        android:layout_marginBottom="54dp"
        android:layout_marginRight="230dp"
        android:layout_toLeftOf="@+id/btn_yourPreferences"
        android:background="@drawable/signedicon" />

    <Button
        android:id="@+id/btn_signUp"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_alignLeft="@+id/btn_yourCompetitions"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="36dp"
        android:background="@drawable/yourcompeteicon" />

    <Button
        android:id="@+id/btn_listCompetitions"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_alignBaseline="@+id/btn_yourCompetitions"
        android:layout_alignBottom="@+id/btn_yourCompetitions"
        android:layout_alignLeft="@+id/btn_yourPreferences"
        android:background="@drawable/listicon"
        android:onClick="onClickViewSearch"
         />

</RelativeLayout>

===============>>#1 票数:2 已采纳

请执行下列操作。 我刚刚对你的代码进行了一些细微的修改

是profile.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/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />

gradient.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:angle="90"
        android:centerColor="#880d0d0f"
        android:endColor="#885d5d5e"
        android:startColor="#880f0f10" />
</shape>

main_layout.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"
android:background="@drawable/background"
tools:context=".Main" >

<ImageButton
    android:id="@+id/btn_yourPreferences"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_alignBottom="@+id/btn_signUp"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="26dp"
    android:layout_marginRight="275dp"
    android:background="@drawable/profile"
    android:background="@drawable/profileicon"
    android:onClick="onClickViewPreferences" />

<ImageButton
    android:id="@+id/btn_yourCompetitions"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_above="@+id/btn_yourPreferences"
    android:layout_marginBottom="54dp"
    android:layout_marginRight="230dp"
    android:layout_toLeftOf="@+id/btn_yourPreferences"
    android:background="@drawable/profile"
    android:src="@drawable/signedicon" />

<ImageButton
    android:id="@+id/btn_signUp"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_alignLeft="@+id/btn_yourCompetitions"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="36dp"
    android:background="@drawable/profile"
    android:src="@drawable/yourcompeteicon" />

<ImageButton
    android:id="@+id/btn_listCompetitions"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_alignBaseline="@+id/btn_yourCompetitions"
    android:layout_alignBottom="@+id/btn_yourCompetitions"
    android:layout_alignLeft="@+id/btn_yourPreferences"
    android:background="@drawable/profile"
    android:src="@drawable/listicon"
    android:onClick="onClickViewSearch"
     />

  ask by Marc Rasmussen translate from so

未解决问题?本站智能推荐: