gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:52:59 25 4
gpt4 key购买 nike

在为我的一个 friend 做一个小项目时,我在使用 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 的情况下实现这一点?

最佳答案

在 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

关于java - 以编程方式垂直对齐 TextView 文本(无 XML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com