在文本视图中添加圆点,就像在收据中一样

Add dots to TextView like in the receipt(在文本视图中添加圆点,就像在收据中一样)

本文介绍了在文本视图中添加圆点,就像在收据中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在收据中实现类似产品列表的东西:
啤酒.20
牛奶.10
带果酱的曲奇.....................15
智能手机10 GB 3 GHz
1 GB内存NFC 10MPx
摄像机..400

说明: 检查信息(啤酒、牛奶)我认为它应该是TextView,我需要用圆点填充。
Money(20,10)是另一个应与ViewGroup右侧对齐的TextView。

有没有办法做到这一点?也许我需要从TextView继承并重写onDraw()或其他什么?
非常感谢您的建议!

推荐答案

我有解决方案。也许它会对某人有所帮助。

  1. 文件check_info_item.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
    <RelativeLayout android:layout_width="match_parent"
              android:layout_height="wrap_content">
    
    <TextView android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:id="@+id/txt_fake_value"
              android:textSize="18dp"
              android:textColor="@android:color/transparent"
              android:layout_alignParentRight="true"/>
    <example.com.CheckInfoTextView android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:id="@+id/txt_fake_info"
              android:textSize="18dp"
              android:textColor="@android:color/transparent"
              android:layout_alignParentLeft="true"
              android:layout_toLeftOf="@id/txt_fake_value"/>
    <TextView android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:id="@+id/txt_check_info_value"
              android:text=""
              android:textSize="18dp"
              android:textColor="#000"
              android:layout_alignParentRight="true"
              android:layout_alignBottom="@id/txt_fake_info"/>
    <example.com.CheckInfoTextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textSize="18dp"
            android:textColor="#000"
            android:id="@+id/txt_check_info"
            android:text=""
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/txt_check_info_value"/>
    </RelativeLayout>
    </LinearLayout>
    
  2. 填写信息字段的代码(在活动中):

        View row = getLayoutInflater().inflate(R.layout.check_info_item, null);
        //Fake fields needed to align base fields in the xml file
        TextView txtFakeValue = (TextView) row.findViewById(R.id.txt_fake_value);
        txtFakeValue.setText(String.valueOf(pair.second));
    
        TextView txtFake = (TextView) row.findViewById(R.id.txt_fake_info);
        txtFake.setText(pair.first);
    
        TextView txtValue = (TextView) row.findViewById(R.id.txt_check_info_value);
        txtValue.setText(String.valueOf(pair.second));
    
        TextView txtTitle = (TextView) row.findViewById(R.id.txt_check_info);
        txtTitle.setText(pair.first);
    
  3. 和CheckInfoTextView:

    public class CheckInfoTextView extends TextView {
    
    
        public CheckInfoTextView(Context context) {
            super(context);
        }
    
        public CheckInfoTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CheckInfoTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasWindowFocus) {
            super.onWindowFocusChanged(hasWindowFocus);
            if(!hasWindowFocus) return;
    
            int requiredDots = getRequiredDotsNumber();
            if(requiredDots == 0) {
                String text = getText().toString();
                StringBuilder result = new StringBuilder();
                result.append(text.substring(0, text.lastIndexOf(' ')));
                result.append("
    ");
                result.append(text.substring(text.lastIndexOf(' ') + 1));
                setText(result.toString());
    
                requiredDots = getRequiredDotsNumber();
            }
            String dots = "";
            for (int i = 0; i < requiredDots; ++i) {
                dots += " .";
            }
            setText(getText() + dots);
        }
    
        private int getRequiredDotsNumber() {
            final int width = getWidth();
            final int lastLineWidth = (int) getLayout().getLineWidth(getLineCount() - 1);
            final int availableWidthForDots = width - lastLineWidth;
            final int widthOfOneDot = getWidthOfOneDot();
            final int widthOfTwoDotsWithSpace = getWidthOfTwoDotsWithSpace();
            final int widthOfSpace = widthOfTwoDotsWithSpace - (widthOfOneDot * 2);
            final int widthOfDotWithSpace = widthOfSpace + widthOfOneDot;
            int numberOfDots = availableWidthForDots / widthOfDotWithSpace;
            return numberOfDots;
        }
    
        private int getWidthOfTwoDotsWithSpace() {
           return getStringWidth(". .");
        }
    
        private int getWidthOfOneDot() {
           return getStringWidth(".");
        }
    
        private int getStringWidth(String text) {
            Rect dotBounds = new Rect();
            getPaint().getTextBounds(text,0,text.length(),dotBounds);
            return dotBounds.width();
        }
    }
    

这篇关于在文本视图中添加圆点,就像在收据中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在文本视图中添加圆点,就像在收据中一样

基础教程推荐