質問: android textviewテキストの位置揃え どのようにしてtextviewのテキストを正当化することができますか(テキストが左右両側で同じ高さになるように)。私は可能な解決策を見つけました ここ 、しかし(垂直中心をcenter_verticalに変更したとしても)うまくいかない。
回答: この問題にはcustomviewがあり、このカスタムテキストビューはjustifiedテキストビューをサポートしています。これで略奪: justifiedtextview import java.util.arraylist; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.typeface; import android.text.textpaint; import android.view.view; public class justifiedtextview extends view { string text; arraylist<line> linescollection = new arraylist<line>(); textpaint textpaint; typeface font; int textcolor; float textsize = 42f, lineheight = 57f, wordspacing = 15f, linespacing = 15f; float onbirim, w, h; float leftpadding, rightpadding; public justifiedtextview(context context, string text) { super(context); this.text = text; init(); } private void init() { textpaint = new textpaint(paint.anti_alias_flag); textcolor = color.black; } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); if (font != null) { font = typeface.createfromasset(getcontext().getassets(), "font/trykker-regular.ttf"); textpaint.settypeface(font); } textpaint.setcolor(textcolor); int minw = getpaddingleft() + getpaddingright() + getsuggestedminimumwidth(); w = resolvesizeandstate(minw, widthmeasurespec, 1); h = measurespec.getsize(widthmeasurespec); onbirim = 0.009259259f * w; lineheight = textsize + linespacing; leftpadding = 3 * onbirim + getpaddingleft(); rightpadding = 3 * onbirim + getpaddingright(); textpaint.settextsize(textsize); wordspacing = 15f; line linebuffer = new line(); this.linescollection.clear(); string[] lines = text.split("\n"); for (string line : lines) { string[] words = line.split(" "); linebuffer = new line(); float linewidth = leftpadding + rightpadding; float totalwordwidth = 0; for (string word : words) { float ww = textpaint.measuretext(word) + wordspacing; if (linewidth + ww + (linebuffer.getwords().size() * wordspacing) > w) {// is linebuffer.addword(word); totalwordwidth += textpaint.measuretext(word); linebuffer.setspacing((w - totalwordwidth - leftpadding - rightpadding) / (linebuffer.getwords().size() - 1)); this.linescollection.add(linebuffer); linebuffer = new line(); totalwordwidth = 0; linewidth = leftpadding + rightpadding; } else { linebuffer.setspacing(wordspacing); linebuffer.addword(word); totalwordwidth += textpaint.measuretext(word); linewidth += ww; } } this.linescollection.add(linebuffer); } setmeasureddimension((int) w, (int) ((this.linescollection.size() + 1) * lineheight + (10 * onbirim))); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawline(0f, 10f, getmeasuredwidth(), 10f, textpaint); float x, y = lineheight + onbirim; for (line line : linescollection) { x = leftpadding; for (string s : line.getwords()) { canvas.drawtext(s, x, y, textpaint); x += textpaint.measuretext(s) + line.spacing; } y += lineheight; } } public string gettext() { return text; } public void settext(string text) { this.text = text; } public typeface getfont() { return font; } public void setfont(typeface font) { this.font = font; } public float getlineheight() { return lineheight; } public void setlineheight(float lineheight) { this.lineheight = lineheight; } public float getleftpadding() { return leftpadding; } public void setleftpadding(float leftpadding) { this.leftpadding = leftpadding; } public float getrightpadding() { return rightpadding; } public void setrightpadding(float rightpadding) { this.rightpadding = rightpadding; } public void setwordspacing(float wordspacing) { this.wordspacing = wordspacing; } public float getwordspacing() { return wordspacing; } public float getlinespacing() { return linespacing; } public void setlinespacing(float linespacing) { this.linespacing = linespacing; } class line { arraylist<string> words = new arraylist<string>(); float spacing = 15f; public line() { } public line(arraylist<string> words, float spacing) { this.words = words; this.spacing = spacing; } public void setspacing(float spacing) { this.spacing = spacing; } public float getspacing() { return spacing; } public void addword(string s) { words.add(s); } public arraylist<string> getwords() { return words; } } } 上記のクラスをsrcフォルダに追加し、このサンプルコードを使用してレイアウトに追加します。justifiedtextview jtv= new justifiedtextview(getapplicationcontext(), "lorem ipsum dolor sit amet... "); linearlayout place = (linearlayout) findviewbyid(r.id.book_profile_content); place.addview(jtv);