Android:无缘无故地点击图片按钮

时间:2013-01-27 00:51:56

标签: java android xml user-interface

我在Android应用中有一个具有左箭头和右箭头按钮的视图。左箭头位于屏幕的左边缘,右侧按钮位于右侧。按下任一按钮时,居中文本视图中的文本应该更改。就这些。我没有设置任何动画或任何东西。但是,在测试中,当按下任一按钮时,两个按钮在各自的方向上移动一点,直到它们离开屏幕。这就像魔术一样。以下是活动的代码:

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) + " sec\n" +
             "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();
}

movel和mover方法作用于左右按钮,但不移动它们。这是接口的xml文件:

<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>

任何人都可以提供任何解释吗?

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为你的textview是动态大小的“wrap_content”,居中,当文本长度增加时,它会将按钮推向一边。您应该尝试固定宽度,例如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"/>