以编程方式垂直对齐TextView文本(无XML)

时间:2015-12-08 23:28:24

标签: java android

为我的一位朋友处理一个小项目,我在使用TextView时无法垂直对齐文本

这是我的Activity类(纯java没有XML):

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;

public class WeightHistory extends AppCompatActivity {

    WeightLog[] history = {new WeightLog(30030, 100, 50), new WeightLog(30030, 50, 55), new WeightLog(30030, 55, 40), new WeightLog(30030, 40, 40)};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView list = new ListView(this);
        list.setAdapter(new MyAdapter(this, history));
        setContentView(list);
    }

    private class MyAdapter extends ArrayAdapter<WeightLog> {

        public MyAdapter(Context context, WeightLog[] logs) {
            super(context, -1, -1, logs);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout listLayout = new LinearLayout(WeightHistory.this);
            listLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));

            WeightLog weightLog = super.getItem(position);
            TextView listText = new TextView(WeightHistory.this);
            listText.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1f));

            listText.setGravity(Gravity.START | Gravity.CENTER);

            listLayout.addView(listText);
           /* if (weightLog.lostWeight() || weightLog.gainedWeight()) {
                listText.setTextColor(Color.WHITE);
                listText.setBackgroundColor(weightLog.lostWeight() ? Color.GREEN : Color.RED);
            }*/
            listText.setText(weightLog.toString());

            return listLayout;
        }
    }

}

如果你很好奇,这里是WeightLog类(列表中显示的类)

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Jonathan on 12/8/2015.
 */
public final class WeightLog {

    private final long time;
    private final double lastWeight, currentWeight;


    public WeightLog(long time, double lastWeight, double currentWeight) {
        this.time = time;
        this.lastWeight = lastWeight;
        this.currentWeight = currentWeight;
    }

    public boolean lostWeight() {
        return currentWeight < lastWeight;
    }

    public boolean gainedWeight() {
        return currentWeight > lastWeight;
    }

    @Override
    public String toString() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        int day = cal.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM");
        String month_name = month_date.format(cal.getTime());
        return "Week of " + month_name + " " + formatDay(day) + ":\t\t\t\t\t\t" + lastWeight + "\t\t\t\t\t\t" + currentWeight + "\t\t\t\t\t\t" + (lastWeight == currentWeight ? "=" : lastWeight < currentWeight ? "+" : "-");
    }

    public String formatDay(int day) {
        int j = day % 10,
                k = day % 100;
        if (j == 1 && k != 11) {
            return day + "st";
        }
        if (j == 2 && k != 12) {
            return day + "nd";
        }
        if (j == 3 && k != 13) {
            return day + "rd";
        }
        return day + "th";
    }
}

以下是我遇到的问题:

Image of issue

如您所见,数字未垂直对齐。

如何在不使用XML的情况下实现这一目标?谢谢!

1 个答案:

答案 0 :(得分:1)

在WeightLog.toString()中,对于这一行:

return "Week of " + month_name + " " + formatDay(day) + 
    ":\t\t\t\t\t\t" + lastWeight + "\t\t\t\t\t\t" + currentWeight + "\t\t\t\t\t\t" + 
    (lastWeight == currentWeight ? "=" : lastWeight < currentWeight ? "+" : "-");

尝试使用String.format()。文档在这里:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)

使用String.format(),这一行将成为以下内容:

return String.format("Week of %s %4s: %10.1f %10.1f %10s", 
            month_name, formatDay(day), lastWeight, currentWeight, 
            (lastWeight == currentWeight ? "=" : 
                lastWeight < currentWeight ? "+" : "-")
            );

我还没有在Android项目中尝试过这个,但在Java控制台程序中,这段代码使得所有数字完美排列。如果这在Android中无法按照您希望的方式运行,则您需要切换到等宽字体或使用XML。我可能错了,但我认为XML是Google希望您使用的解决方案。

您可能需要使用格式字符串来使显示按照您希望的方式显示;文档位于:http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax