当从android中的矩形形状按下时,按钮变为原始形状?

时间:2017-01-16 12:22:58

标签: android button

我有几个按钮,我设计成矩形形状。它们都看起来像我想要的那样...但问题是,当我按下它们......一毫秒后,它们转向原始形状,然后返回自定义(irrectangle。)..我尝试使用不同的代码,但它不工作..我希望我的按钮保持相同(意味着自定义的形状),即使按下。

这是myButton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip"  />
            <gradient android:angle="-90" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="5dip" />
            <stroke android:width="2dip"  />
            <solid android:color="@color/colormain"/>

            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="77dip" />

            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90"

             android:startColor="@color/colormain" android:endColor="@color/colormain" />
        </shape>
    </item>
</selector>

这是我的login.xml ...我正在使用这些按钮

<ScrollView 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:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@android:color/white"
    tools:context="com.example.zeba.broccoli.Login">

    <!-- Login progress -->
    <LinearLayout
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView android:src="@drawable/logobr"
            android:layout_width="wrap_content"
            android:layout_height="72dp"
            android:layout_marginTop="40dp"
            android:layout_gravity="center_horizontal" />





        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Already Member ?"
                android:gravity="center"
                android:textSize="25dp"/>


            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">






                <AutoCompleteTextView
                    android:id="@+id/email"
                    android:layout_width="match_parent"

                    android:hint="email"
                    android:inputType="textEmailAddress"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:background="@drawable/rounded_edittext"

                    android:layout_height="40dp"
                    android:paddingLeft="5dp"

                    android:layout_marginBottom="20dip"/>

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"

                    android:hint="password"
                    android:imeActionId="@+id/login"

                    android:imeOptions="actionUnspecified"
                    android:inputType="textPassword"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:background="@drawable/rounded_edittext"
                    android:layout_height="40dp"
                    android:paddingLeft="5dp"

                    android:layout_marginBottom="20dip"/>

            </android.support.design.widget.TextInputLayout>

            <Button
                android:id="@+id/lbtn"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="Login"
                android:background="@drawable/mybutton"

                android:textColor="#fff"
                android:textStyle="bold"
                android:elevation="0dp" />

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:weightSum="1" >
                <TextView
                    android:id="@+id/fpss"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:layout_marginTop="16dp"
                    android:text="Forgot Password" />
                <TextView
                    android:id="@+id/cAcc"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:layout_marginTop="16dp"
                    android:textColor="#FFC107"

                    android:text="Create An account" />



            </LinearLayout>


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:text="Or"
                    android:textSize="22dp"
                    android:textColor="#c4bb99"/>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="16dp"
                    android:layout_toLeftOf="@id/tvText"
                    android:background="#c0c0c0"
                    />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="16dp"
                    android:layout_toRightOf="@id/tvText"
                    android:background="#c0c0c0"
                    />

            </RelativeLayout>



            <com.facebook.login.widget.LoginButton
                android:id="@+id/login_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:paddingTop="15dp"
                android:paddingBottom="15dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:background="@drawable/mybutton1"
                android:layout_gravity="center_horizontal"
                android:text="  connect_with_facebook" />


            <!-- Login Button in the same style as LikeView -->

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:background="#c0c0c0"
                android:layout_marginTop="16dp"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Order Without An Account "
                android:gravity="center"
                android:textSize="18dp"
                android:textStyle="bold"/>


            <Button
                android:id="@+id/gbtn"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@drawable/mybutton2"
                android:text="As a Guest"

                android:textStyle="bold"
                android:textColor="#fff"
                android:elevation="0dp" />



        </LinearLayout>

        <ProgressBar
            android:id="@+id/login_progress"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:visibility="gone" />

    </LinearLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:1)

为按下状态写的代码应该与默认状态相同。如果你想在按下状态下使用相同的大小

所以myButton.xml将是

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item  android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="77dip" />

            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90"

                      android:startColor="@color/colorPrimary" android:endColor="@color/colorPrimary" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="77dip" />

            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90"

                      android:startColor="@color/colorPrimary" android:endColor="@color/colorPrimary" />
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="77dip" />

            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90"

                      android:startColor="@color/colorPrimary" android:endColor="@color/colorPrimary" />
        </shape>
    </item>
</selector>