Android button loses text alignment after setText

余生颓废 提交于 2020-01-23 12:52:13

问题


I'm using xml to draw a spinning progress indicator along with some text. In the bottom of the screen I have a TableLayout with two buttons, which are centered in the page with each text also centered.

<RelativeLayout
        android:id="@+id/progresscontainer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal">


        <ProgressBar android:id="@+id/progress_bar" 
            style="?android:attr/progressBarStyleSmall" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:paddingRight="10dp" />

        <TextView android:id="@+id/progress_text" 
            android:layout_toRightOf="@id/progress_bar"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="First text" />

</RelativeLayout>

<TableLayout
    android:id="@+id/buttonbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:paddingTop="3dip"
    android:background="@color/buttonbar"
    android:stretchColumns="0,1">

    <TableRow>

        <Button android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button1" />

        <Button android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button2" />

    </TableRow>

</TableLayout>

In the code I have a runnable that after a couple of seconds changes the text on the progress_text TextView.

private Runnable mTimeTask = new Runnable() {

   public void run() {
       TextView progressText = (TextView) findViewById(R.id.progress_text);
       progressText.setText("Second text");
   }

};

The problem is that after this setText(), once I focus on one of the buttons, the text loses it's centered alignment and goes all the way to the left. What am I doing wrong?


回答1:


I had this problem too. It seems that setText() changes the gravity settings, so I had to reapply them using:

button.setGravity(Gravity.CENTER_HORIZONTAL);



回答2:


Try to reset it's padding to:

button.setPadding(left, top, right, bottom);

With value:

button.setPadding(0, 0, 0, 0);



回答3:


None of the answers here worked for me so I tried to find my own way to fix it and was messing with a TextView that has the same alignment problem but when I added the line android:maxLines="1" it suddenly worked so I tried it on the button and it worked.

Just add the android:maxLines="1" to the definition of your button and the text alignment should stay after setting a new text.



来源:https://stackoverflow.com/questions/4195426/android-button-loses-text-alignment-after-settext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!