我試過使用String.format("%-10s %s %20s",stringPlaceNumber, userName, scoreNumberAsString);
我得到以下輸出 High Scores Page Image。如何在一些字符串的長度不同時格式化幾個字符串以獲得等間距
如何使用正確數量的空格或隱形字符進行填充以使其看起來間隔適當?
其他信息: - 這是在Android上,所以在終端上試試這個在這裏沒有用。的字體具有可變的寬度(即,空間可以比字符小)
- 用戶的名字來自被輸入到SQLite數據庫一個Android的EditText和
BaseAdapter
把最高級10分入ListView
。 - 用戶可以在遊戲結束時輸入分數的名稱,最多爲個字符。
- 這是代碼
`listView.setAdapter(新BaseAdapter(){ @覆蓋 公衆詮釋getCount將(){ 返回scores.size()的其餘部分; }
@Override
public ScoreManager.Score getItem(int position) {
return scores.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ScoreManager.Score score = getItem(position);
TextView view = new TextView(ScoresActivity.this);
view.setText(String.format("%-10s %s %20s", getPlaceNumber(position+1),score.name.trim(), formatScore(score.score).trim()));
view.setTextSize(5 * getResources().getDisplayMetrics().density);
return view;
}
});
>空格或不可見的字符,使其看起來適當間隔。 1.你的意思是在Listview中,爲什麼不使用對齊列表視圖項呢? 2.關於你的格式字符串,我測試過了,它正確輸出字符串。 – kidnan1991
從您的圖像看起來就像您需要一個雙單元的桌子;爲什麼只有1?這看起來像一個XY問題,'String.format()'不會幫你在這裏,尤其是因爲你使用的是可變寬度的字體 – fge
我最終有點作弊。現在我將使用固定寬度的字體。 –