2015-12-08 72 views
2

上的一個小項目我的一個朋友的工作和我使用一個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"; 
    } 
} 

這裏類是我遇到的問題:

Image of issue

正如你所看到的數字不垂直對齊。

如何在不使用XML的情況下實現此目的?謝謝!

回答

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

+0

是的我以前用Java在調整輸出在控制檯,但它不適用於Android的不幸。 –

+0

2個問題:1)當你在Android中嘗試時出現了什麼問題? 2)是否有你沒有使用XML進行格式化的原因? –

+0

好的,我現在可以看到問題了。我嘗試使用默認字體String.format(),並沒有正確排列,因爲空格的大小與數字字符的大小不同。嘗試listText.setTypeface(Typeface.MONOSPACE);這將設置一個等寬字體。它看起來很醜,但數字正確。 –