【问题标题】:Button's text gets wrong alignment after a setText() call on a TextView在 TextView 上调用 setText() 后,按钮的文本对齐错误
【发布时间】:2015-02-14 18:21:24
【问题描述】:

在我的例子中,我有一个 activity 和一个 TableLayout,其中包含一些视图,例如一个 ID 为“Number1”的 Button 和一个 ID 为“密码”的 TextView

当我点击按钮时,它的事件处理程序被调用,它所做的一件事就是设置密码TextView 文本。在此之后,按钮的文本变得未对齐 - 而不是原来的中心对齐,按钮内的文本将在底部对齐。

我使用的是 Android Studio 1.1 Beta 4,项目的 compileSdkVersion 设置为 21,minSdkVersion 设置为 19,targetSdkVersion 设置为 21。

下面我提供了相关代码的sn-ps。

事件处理程序 sn-p ...

public void buttonNumberClick(View view) {

    TextView passwordText = (TextView) findViewById(R.id.password);

    Button sourceButton = (Button)view;

    String sourceText = sourceButton.getText().toString();
    String text = passwordText.getText().toString();
    if (text.length() < 4) {
        int number = Integer.parseInt(sourceText);
        String newText = text + Integer.toString(number);

        // After this below line, the button text of the clicked button (Number1) becomes bottom aligned.
        passwordText.setText(newText);

    }
}

布局 sn-p ...

<TableLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <TableRow
        android:minHeight="130dp"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <LinearLayout
            android:layout_weight="0.5"
            android:layout_row="0"
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="vertical">

            <TextView
                android:layout_gravity="center_horizontal"
                android:id="@+id/enterPinTextView"
                android:textSize="25sp"
                android:text="Enter PIN"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_gravity="center_horizontal"
                android:id="@+id/password"
                android:textSize="25sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <RelativeLayout
                android:layout_gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ProgressBar
                    android:id="@+id/progressRing"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_centerInParent="true"
                    android:progressDrawable="@drawable/circular_progress_bar" />

                <TextView
                    android:text="5.0"
                    android:id="@+id/progressRingText"
                    android:textSize="20sp"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RelativeLayout>

        </LinearLayout>

    </TableRow>

    <TableRow
        android:gravity="bottom|center"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableRow
                android:layout_weight="0.25"
                android:layout_height="0dp"
                android:layout_width="wrap_content">

                <Button
                    android:id="@+id/buttonTest"
                    android:text="Test"
                    android:minHeight="50dp"
                    android:layout_width="72dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="0dp"
                    android:onClick="buttonTestClick" />

                <Button
                    android:id="@+id/Number1"
                    android:text="1"
                    android:minHeight="50dp"
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="0dp"
                    android:onClick="buttonNumberClick" />

            </TableRow>

        </TableLayout>

    </TableRow>

</TableLayout>

【问题讨论】:

  • 没有评论的反对票。什么都不感谢。

标签: android


【解决方案1】:

在查看了 Stack Overflow 上的一些类似事件(如 this)之后,我尝试了一些不同的方法,包括以编程方式设置 Button 的文本以及再次设置其重力和填充。这些都不起作用。

我通过反复试验得出了这个解决方案 - 在对 setText 的违规调用之前和之后隐藏然后显示按钮。另外,请注意,您必须将可见性设置为 GONE 而不是 INVISIBLE。这可能会迫使布局引擎以正确的对齐方式再次创建按钮。

如下所示的新事件处理程序。

public void buttonNumberClick(View view) {

    TextView passwordText = (TextView) findViewById(R.id.password);

    Button sourceButton = (Button)view;

    String sourceText = sourceButton.getText().toString();
    String text = passwordText.getText().toString();
    if (text.length() < 4) {
        int number = Integer.parseInt(sourceText);
        String newText = text + Integer.toString(number);

        // Fix the issue of the button text being mis-aligned after the below call to setText.
        sourceButton.setVisibility(View.GONE);

        passwordText.setText(newText);

        // Fix the issue of the button text being mis-aligned after the above call to setText.
        sourceButton.setVisibility(View.VISIBLE);
    }
}

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 2014-11-21
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多