我是新來的android編程,我正在開發一個財務管理應用程序。目前我進入了setText()方法破壞了我傳遞的字符串格式問題Android:setText破壞格式化字符串
我有有它返回的帳戶信息的格式化字符串習慣toString方法的Account類:
public String toString(int colWidth1, int colWidth2, int colWidth3) {
return String.format("%-" + colWidth1 + "s", myDisplayName) +
String.format("%-" + colWidth2 + "s", myBalance) +
String.format("%-" + colWidth3 + "s", myInterestRate);
}
而在AccountInfo活動,我有:
// create a table that contains all account information of the current user
TableLayout accountTable = (TableLayout) findViewById(R.id.tableLayout_account_details);
List<Account> accountList = accountManager.getAllAccounts(CurrentUser.getCurrentUser().getUserName());
// for each account, display name, balance and interest rate
for (int i = 0; i < accountList.size(); i++) {
Account account = accountList.get(i);
TableRow accEntry = new TableRow(this);
Button accButton = new Button(this);
accButton.setText(account.toString(10, 10, 10));
Log.i("account info", account.toString(10, 10, 10));
// button format
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, -7, 0, -10);
accButton.setLayoutParams(params);
accButton.setGravity(Gravity.LEFT);
// add button to the row
accEntry.addView(accButton);
// add row to the table
accountTable.addView(accEntry);
// ...more code here for button click listener
}
雖然我logcat的印刷品很好地格式化字符串,因爲我想,在按鈕上的文本不排隊很好
(剛剛意識到我沒有足夠的聲譽發佈我的應用程序的屏幕截圖...)
我試過調試很久但仍然沒有線索。任何幫助深表感謝!
編輯:截圖
我認爲你應該檢查此鏈接瞭解如何使用的String.Format http://stackoverflow.com/questions/3695230/how-to-use-java-string-format –
@CuongHuynh以及有什麼我做雖然錯了? – TheInvisibleFist
我的錯。你遇到問題的列/ textview寬度? –