繁体   English   中英

Android:无缘无故点击图像按钮即可移动

[英]Android: Image buttons move when clicked for no reason

我在具有左箭头和右箭头按钮的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>

谁能提供任何解释?

发生这种情况是因为您的文本视图大小居中,以“ 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"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM