安卓按键的setText后失去文本对齐 [英] Android button loses text alignment after setText
问题描述
我使用XML来绘制一个旋转的进度指示器以及一些文本。在屏幕的底部我有两个按钮,它们集中在页面与每个文本也居中一个TableLayout。
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>
在code我有一个可运行的,经过几秒钟的变化对progress_text TextView的文本。
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");
}
};
现在的问题是,这之后的setText(),一旦我专注于其中一个按钮,文本将丢失它的居中对齐,去一路向左。我究竟做错了什么?
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?
推荐答案
我有这个问题了。看来,的setText()
改变重力的设置,所以我不得不用重新申请他们:
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);
这篇关于安卓按键的setText后失去文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!