0

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

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

4

1 回答 1

2

发生这种情况是因为您的文本视图是动态大小的“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"/>
于 2013-01-27T00:55:53.840 回答