上的一個小項目我的一個朋友的工作和我使用一個TextView對齊的TextView文本編程方式垂直(無XML)
這裏的時候遇到麻煩垂直對齊文本是我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";
}
}
這裏類是我遇到的問題:
正如你所看到的數字不垂直對齊。
如何在不使用XML的情況下實現此目的?謝謝!
是的我以前用Java在調整輸出在控制檯,但它不適用於Android的不幸。 –
2個問題:1)當你在Android中嘗試時出現了什麼問題? 2)是否有你沒有使用XML進行格式化的原因? –
好的,我現在可以看到問題了。我嘗試使用默認字體String.format(),並沒有正確排列,因爲空格的大小與數字字符的大小不同。嘗試listText.setTypeface(Typeface.MONOSPACE);這將設置一個等寬字體。它看起來很醜,但數字正確。 –