对多个按钮使用相同的xml文件

时间:2013-04-16 01:40:19

标签: android button android-animation

我的主导航菜单有四个按钮,所有这四个按钮都包含一个图像。我创建了以下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 个答案:

答案 0 :(得分: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"
     />