文本左右对齐排版--shell

文本左右对齐排版

有文本4.txt如下:

111111111111111111111 98912 张三
222222222222222222 150020 李四四
333333333333333333333 360000 王五
444444444444444444 2332 赵六六
555555555555555555 222 田七
666666666666666666666 999999

简单的文本通过批处理排版

这里写图片描述

编写代码


#! /bin/bash


file=./4.txt

echo -e "\n左对齐\n"

while read line
do
        printf "%-30s %-10d %-10s\n" ${line}
done < ${file}

echo -e "\n右对齐\n"

while read line
do
        printf "%30s %10d %10s\n" ${line}
done < ${file}

运行结果

这里写图片描述

参考文档

文件读取:http://www.cnblogs.com/iloveyoucc/archive/2012/07/10/2585529.html

echo转义:http://www.runoob.com/linux/linux-shell-echo.html

printf 命令:http://www.runoob.com/linux/linux-shell-printf.html

TextView如何排版左右对齐

08-13

请问各位有没有关于TextView更好的排版方法?要左右对齐rn首先先上我这里的代码:rn在判断标点符号的时候是把全角变为半角,当显示不完全时,会用......省略号代替rnrnimport java.util.ArrayList;rnimport java.util.List;rnrnimport android.content.Context;rnimport android.content.res.TypedArray;rnimport android.graphics.Canvas;rnimport android.text.Layout;rnimport android.text.Layout.Alignment;rnimport android.text.StaticLayout;rnimport android.text.TextUtils.TruncateAt;rnimport android.util.AttributeSet;rnimport android.widget.TextView;rnrnpublic class EllipsizingTextView extends TextView rn private static final String TAG = "EllipsizeText";rn private static final boolean enableLog = (false && Const.DEBUG);rn private static final String ELLIPSIS = "......";rnrn public interface EllipsizeListener rn void ellipsizeStateChanged(boolean ellipsized);rn rnrn private final List ellipsizeListeners = new ArrayList();rn private boolean isEllipsized;rn private boolean isStale;rn private boolean programmaticChange;rn private String fullText;rn private int maxLines;rn private float lineSpacingMultiplier = 1.0f;rn private float lineAdditionalVerticalPadding = 0.0f;rnrn public EllipsizingTextView(Context context) rn this(context, null);rn rnrn public EllipsizingTextView(Context context, AttributeSet attrs) rn this(context, attrs, 0);rn rnrn public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) rn super(context, attrs, defStyle);rn super.setEllipsize(null);rn TypedArray a = context.obtainStyledAttributes(attrs, new int[] android.R.attr.maxLines );rn setMaxLines(a.getInt(0, Integer.MAX_VALUE));rn rnrn public void addEllipsizeListener(EllipsizeListener listener) rn if (listener == null) rn throw new NullPointerException();rn rn ellipsizeListeners.add(listener);rn rnrn public void removeEllipsizeListener(EllipsizeListener listener) rn ellipsizeListeners.remove(listener);rn rnrn public boolean isEllipsized() rn return isEllipsized;rn rnrn @Overridern public void setMaxLines(int maxLines) rn super.setMaxLines(maxLines);rn this.maxLines = maxLines;rn isStale = true;rn rnrn public int getMaxLines() rn return maxLines;rn rnrn public boolean ellipsizingLastFullyVisibleLine() rn return maxLines == Integer.MAX_VALUE;rn rnrn @Overridern public void setLineSpacing(float add, float mult) rn this.lineAdditionalVerticalPadding = add;rn this.lineSpacingMultiplier = mult;rn super.setLineSpacing(add, mult);rn rnrn @Overridern protected void onTextChanged(CharSequence text, int start, int before,rn int after) rn super.onTextChanged(text, start, before, after);rn if (!programmaticChange) rn fullText = text.toString();rn isStale = true;rn rn rnrn @Overridern protected void onSizeChanged(int w, int h, int oldw, int oldh) rn super.onSizeChanged(w, h, oldw, oldh);rn if (ellipsizingLastFullyVisibleLine()) rn isStale = true;rn rn rnrn public void setPadding(int left, int top, int right, int bottom) rn super.setPadding(left, top, right, bottom);rn if (ellipsizingLastFullyVisibleLine()) rn isStale = true;rn rn rnrn @Overridern protected void onDraw(Canvas canvas) rn if (isStale) rn resetText();rn rn super.onDraw(canvas);rn rnrn private void resetText() rn String workingText = null;rn workingText = padString(fullText);rn if (enableLog) Common.traceLog('d', TAG, "fullText= " + fullText.length() + " workingText= " + workingText.length());rnrn boolean ellipsized = false;rn Layout layout = createWorkingLayout(workingText);rn int linesCount = getLinesCount(); rn if (enableLog) Common.traceLog('d', TAG, "layout.getLineCount()= " + layout.getLineCount() + " linesCount= " + linesCount);rnrn if (layout.getLineCount() > linesCount) rn // We have more lines of text than we are allowed to display.rn// workingText = workingText.substring(0, layout.getLineEnd(linesCount - 1)).trim();rn workingText = workingText.substring(0, layout.getLineEnd(linesCount - 1));rn// if (enableLog) Common.traceLog('d', TAG, "workingText= " + workingText + " length= " + workingText.length());rnrn while (createWorkingLayout(workingText + ELLIPSIS).getLineCount() > linesCount) rn// int lastSpace = workingText.lastIndexOf(' '); // Chinese has no ' ' as separatorrn// if (lastSpace == -1) rn// break;rn// rn// workingText = workingText.substring(0, lastSpace);rn // Chinese has no ' ' as separator, so truncate 1 by 1 and tryrn workingText = workingText.substring(0, workingText.length() - 1); rn// if (enableLog) Common.traceLog('d', TAG, "workingText= " + workingText);rn rnrn workingText = workingText.substring(0, workingText.length() - 1); rn workingText = workingText + ELLIPSIS;rn if (enableLog) Common.traceLog('d', TAG, "workingText= " + workingText);rn ellipsized = true;rn rn if (!workingText.equals(getText())) rn programmaticChange = true;rn try rn setText(workingText);rn finally rn programmaticChange = false;rn rn rn isStale = false;rn if (ellipsized != isEllipsized) rn isEllipsized = ellipsized;rn for (EllipsizeListener listener : ellipsizeListeners) rn listener.ellipsizeStateChanged(ellipsized);rn rn rn rnrn /**rn * Get how many lines of text we are allowed to display.rn */rn private int getLinesCount() rn if (ellipsizingLastFullyVisibleLine()) rn int fullyVisibleLinesCount = getFullyVisibleLinesCount();rn if (fullyVisibleLinesCount == -1) rn return 1;rn else rn return fullyVisibleLinesCount;rn rn else rn return maxLines;rn rn rnrn /**rn * Get how many lines of text we can display so their full height is visible.rn */rn private int getFullyVisibleLinesCount() rn Layout layout = createWorkingLayout("");rn int height = getHeight() - getPaddingTop() - getPaddingBottom();rn int lineHeight = layout.getLineBottom(0);rn// if (enableLog) Common.traceLog('d', TAG, "height= " + height + " lineHeight=" + lineHeight);rn return height / lineHeight;rn rnrn private Layout createWorkingLayout(String workingText) rn StaticLayout s = new StaticLayout(workingText, getPaint(),rn getWidth() - getPaddingLeft() - getPaddingRight(),rn Alignment.ALIGN_NORMAL, lineSpacingMultiplier,rn lineAdditionalVerticalPadding, false /* includepad */);rn return s;rn rnrn @Overridern public void setEllipsize(TruncateAt where) rn // Ellipsize settings are not respectedrn rn rn // pad the input string so it can be formed properly later on, hopefullyrn // android StaticLayout has difficulty in formating Chinese punctuation properlyrn private String padString(String inStr) rn int inLength = inStr.length();rn char[] inChar = inStr.toCharArray();rn char[] outChar = new char[inLength*3];rn int outIndex = 0;rn for(int i=0; i

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试