Android: Image buttons move when clicked for no reason

I have a certain view in an android app that has a left arrow and right arrow button. The left arrow is on the left edge of the screen and the right button is on the right side. When either button is pressed, the text in a centered text view is supposed to change. That is all. I did not set any animations or anything. However, in testing, when either button is pressed, both buttons move a little in their respective directions until they are off the screen. It's like magic. Here is the code for the activity:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stat_page);
    this.current = 1;

    this.setStats();
}

public void setStats()
{
    GameTable entry = new GameTable(this);
    entry.open();

    //Check for an empty table

    if(entry.isEmpty())
    {
        entry.createRows();
    }

    //Retrieve data from the matching row

    int[] info = entry.getStats(this.current);

    //Present the data in the GUI
    TextView title = (TextView) findViewById(R.id.statstitle);
    TextView stats = (TextView) findViewById(R.id.statpresents);

    title.setText(this.diffString());
    title.setTextSize(16);
    title.setTextColor(Color.BLACK);

    String present = "High Score: " + info[0] + "n" +
             "Longest Time: " + ((int)info[1] / 1000) + " secn" +
             "Total Adds: " + info[2] + "n" +
             "Total Subtracts: " + info[3] + "n" +
             "Total Products: " + info[4] + "n" +
             "Total Divides: " + info[5] + "n" +
             "Total Squared: " + info[6] + "n" +
             "Total Square Roots: " + info[7] + "n";

    stats.setText(present);
    stats.setTextSize(15);
    stats.setTextColor(Color.BLACK);

    entry.close();
}

private String diffString()
{
    if(this.current == 1)
    {
        return "Freshman Stats";
    }
    else if(this.current == 2)
    {
        return "Sophomore Stats";
    }
    else if(this.current == 3)
    {
        return "Junior Stats";
    }
    else
    {
        return "Senior Stats";
    }
}

public void movel(View view)
{
    this.current--;

    if(this.current == 0)
    {
        this.current = 4;
    }

    this.setStats();
}

public void mover(View view)
{
    this.current++;

    if(this.current == 5)
    {
        this.current = 1;
    }

    this.setStats();
}

the movel and mover methods act on the left and right buttons, but do not move them. Here is the xml file for the interface:

<LinearLayout 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/blackboard"
android:baselineAligned="true"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:orientation="horizontal"
    android:baselineAligned="false" 
    android:gravity="center_horizontal" >

    <ImageButton
        android:id="@+id/imageButtonl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/buttoninvisible"
        android:src="@drawable/leftarrow" 
        android:onClick="movel" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="4"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1">

        <TextView
        android:id="@+id/statstitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded2" 
        android:layout_gravity="center"
        android:layout_centerInParent="true"
        android:padding="10dp"/>

    </RelativeLayout>

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="3">

        <TextView
        android:id="@+id/statpresents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded2" 
        android:layout_gravity="center"
        android:layout_centerHorizontal="true"
        android:padding="10dp"/>

    </RelativeLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:orientation="horizontal"
    android:gravity="center_horizontal" >

    <ImageButton
        android:id="@+id/imageButtonr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/buttoninvisible"
        android:src="@drawable/rightarrow" 
        android:onClick="mover" />

</LinearLayout>

Can anyone provide any kind of explanation?


This is happening because your textview is dyanmically sized "wrap_content",centered and when text length increases it pushes the buttons to side. You should try a fixed width for example 200dp

<TextView
        android:id="@+id/statpresents"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded2" 
        android:layout_gravity="center"
        android:layout_centerHorizontal="true"
        android:padding="10dp"/>
链接地址: http://www.djcxy.com/p/42602.html

上一篇: 如何在缩放前查找缩放点?

下一篇: Android:图片按钮在无故被点击时移动