开发者

When changing textview, the text on a button below the textview moves down. Help!

Pretty much I have a Textview that is acting as a display for asterisks when a user clicks a pin pad of Buttons I made.

The buttons have their text centered, but it seems when I append something to the Textview text property the number 2 pin's layout changes to center at the bottom instead of the dead center. This is part part of the xml:

<!-- PIN text -->
<TableLayout>
    <TableRow android:id="@+id/btn_row1"
        android:gravity="center"
        android:layout_marginTop="6dip">
        <!-- password text-->  
        <TextView
            android:id="@+id/txt_pin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:text=""
            android:background="@drawable/txt_background"/>
    </TableRow>
</TableLayout>  
<!-- PIN ends -->
<!-- Header end -->
<!-- PIN button starts -->      
<TableLayout android:id="@+id/table_modes"
    android:layout_marginTop="6dip">
    <!-- First button rows -->  
    <TableRow android:id="@+id/btn_row1"
        android:gravity="center">
        <!-- one button-->  
        <Button android:id="@+id/btn_one"
            android:layout_column="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dip"
            android:layout_marginRight="2.5dip"
            android:textColor="#FFFFFF"
            android:text="1"
            android:background="@drawable/bu开发者_如何学JAVAtton_layout_med" 
            android:onClick="input"/>
        <!-- two Button -->  
        <Button android:id="@+id/btn_two"
            android:layout_column="2"
            android:layout_width="100dip"
            android:layout_height="60dip"
            android:layout_marginLeft="2.5dip"
            android:layout_marginRight="2.5dip"
            android:gravity="center"
            android:textColor="#FFFFFF"
            android:text="2"
            android:background="@drawable/button_layout_med" 
            android:onClick="input"/>
        <!-- 3 button -->  
        <Button android:id="@+id/btn_three"
            android:layout_column="3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2.5dip"
            android:layout_marginRight="3dip"
            android:textColor="#FFFFFF"
            android:text="3"
            android:background="@drawable/button_layout_med" 
            android:onClick="input"/>
    </TableRow>
    <!-- First row ends -->
 <!-- Rows continue down 3 more then a submit and back button -->

The next part is the java code for OnClick:

public void input(View v){
            Button i = (Button) v;
    TextView tv_pin = (TextView) findViewById(R.id.txt_pin);


    while(view_pin.length() < 10){
        pin.append(i.getText().toString());
        view_pin.append('*');
        tv_pin.setText(view_pin.toString());
        break;
    }

}

Where pin and view_pin are private static variables.


To fix this problem I simply had to change the XML code for height and width for the second button to "wrap_content" instead of giving them a static value.

<!-- two Button -->  
<Button android:id="@+id/btn_two"
    android:layout_column="2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2.5dip"
    android:layout_marginRight="2.5dip"
    android:gravity="center"
    android:textColor="#FFFFFF"
    android:text="2"
    android:background="@drawable/button_layout_med" 
    android:onClick="input"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜