java - Android:单击时图像按钮无故移动

标签 java android xml user-interface

我在 Android 应用程序中有一个特定的 View ,它有一个向左箭头和向右箭头按钮。左箭头位于屏幕的左边缘,右按钮位于屏幕的右侧。当按下任一按钮时,居中 TextView 中的文本应该会发生变化。就这些。我没有设置任何动画或任何东西。然而,在测试中,当按下任一按钮时,两个按钮都会向各自的方向移动一点,直到它们离开屏幕。这就像魔法一样。以下是该 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) + " 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 方法作用于左右按钮,但不移动它们。这是该接口(interface)的 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>

谁能提供任何解释?

最佳答案

发生这种情况是因为您的 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"/>

关于java - Android:单击时图像按钮无故移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14543281/

相关文章:

java - Mopub:将 inmobi 原生广告与适配器 InMobiNative 集成

xml - 子选择中的 oracle xmlcast(xmlquery)

android - LinearLayout 无法转换为 CoordinatorLayout

java - 在android中通过java代码重新排列RelativeLayout子级

java - SpringUtilities无法解析

java - Richfaces 数据表 PDF 导出

java - 如何检查 JTextField 是否为空?

android - 如何抑制由未能生成的类引起的代码生成错误?

javascript - 类型错误 'undefined' 不是对象(评估 'navigator.connection.type' )

java - 将返回方法从 boolean 更改为 int