我只需要在listView中我的产品旁边有一个价格,就需要按照以下方式进行调整,
左侧的产品名称,以及;
右边的价格
这是代码:
public ListView lstMenuItems;
public String[] menuItems = {"Fillet On The Bone", "Rump", "Sirloin", "French Cut Pork Cutlets", "Spatchcock Chicken", "Sticky Smoked Paprika Chicken Wings", "Craft Beer Battered Fish", "Butter Chicken", "Pomegranate & Chilli Marinated Lamb Skewer", "Pork Belly Skewer", "Harissa Basted Chicken Skewer"};
public double[] itemPrice = {135, 99, 99, 99, 89, 79, 60, 75, 99, 95, 69};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
TitleBar(waiterName);
GetListViewValue();
menuItems = AddPad(menuItems, itemPrice);
MenuList();
}
public void MenuList() {
lstMenuItems = (ListView)findViewById(R.id.lvProducts);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.category_list, menuItems);
lstMenuItems.setAdapter(adapter);
}
public String[] AddPad(String[] menu, double[] price) {
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.getDefault());
for (int i = 0; i < menu.length; i++) {
menu[i] = String.format("%-60s %s",menu[i], format.format(price[i]));
}
return menu;
}
String.format
类似乎不起作用。有没有办法做到这一点?请帮忙
最佳答案
创建您的自定义适配器。在getView中,您可以自定义所需的行。您可以为其扩展BaseAdapter。
关于java - 如何在listView中同时向左和向右对齐文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38194830/