Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将点添加到文本视图_Android - Fatal编程技术网

Android 将点添加到文本视图

Android 将点添加到文本视图,android,Android,我想实现支票(支票)信息之类的功能,例如: 啤酒。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。20 牛奶10 果酱饼干。。。。。。。。。。。。。。。。。。。。。15 智能手机10GB 3GHz 1GB内存NFC 10MPx 摄像机。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。四百 说明: 检查信息(啤酒、牛奶)我认为应该是文本视图,我需要用点填充它。 Money(20,10)是另一个文本视图,应该与ViewGroup右侧对齐 有什么办法吗

我想实现支票(支票)信息之类的功能,例如:
啤酒。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。20
牛奶10
果酱饼干。。。。。。。。。。。。。。。。。。。。。15
智能手机10GB 3GHz
1GB内存NFC 10MPx
摄像机。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。四百

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

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

如果“.”(点)是您的基本要求,那么您可以这样尝试

    int label_len = 10;//Edit as per your requirement
     String MoneyValue = "20";
    TextView tv = (TextView)findViewById(your id);
    tv.setText("Beer");

    if(tv.getText().length() < label_len){
        for(int i = tv.getText().length(); i < label_len; i++){

           tv.setText(tv.getText()+".");
        }
    }
tv.setText(tv.getText() + MoneyValue);
我的建议是,只使用I
TextView
来适应啤酒……10(挂接装置)

像这样试试

    int label_len = 10;//Edit as per your requirement
     String MoneyValue = "20";
    TextView tv = (TextView)findViewById(your id);
    tv.setText("Beer");

    if(tv.getText().length() < label_len){
        for(int i = tv.getText().length(); i < label_len; i++){

           tv.setText(tv.getText()+".");
        }
    }
tv.setText(tv.getText() + MoneyValue);
int-label\u-len=10//根据您的要求进行编辑
字符串MoneyValue=“20”;
TextView tv=(TextView)findViewById(您的id);
tv.setText(“啤酒”);
if(tv.getText().length()
这是一个带有硬编码值的示例,您可以尝试动态添加


希望这有帮助…

您可以通过仅使用layout.xml来实现这一点

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"
    android:![enter image description here][2]layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:weightSum="1">    
<TextView android:layout_height="wrap_content"
          android:layout_width="0dp"
          android:layout_weight="0.9"          
          android:text="Milk.................................................................................................................................." />
<TextView android:layout_height="wrap_content"
          android:layout_width="0dp"
          android:layout_weight="0.1"             
          android:text="20" />
</LinearLayout>
</LinearLayout>


此解决方案适用于具有不同分辨率的所有设备。

您应该为每行创建三个文本视图,一个一个地排列:

  • 带有产品名称的文本视图(例如“beer”)
  • 带点的文本视图
  • 带编号的文本视图(例如“20”)
  • 然后把一个产品放在一行。行应为相对布局,其中:

    • TextView no.1与左边缘对齐,宽度设置为包裹内容
    • TextView no.3与右边缘对齐,宽度设置为包裹内容
    • TextView no.2是指电视3号和电视1号,它应该在XML文件中填充大量的点。这是永远不会改变的

    这是可行的,因为2号电视的宽度会缩小,并且总是在产品名称和价格之间。相信我:)

    使用相对布局:

    TextView View TextView
    
    第一个选项将宽度设置为包裹内容,并位于前一行的文本视图下方。
    第二个视图,设置在第一个文本视图的右侧,并使用可绘制的背景,其中点水平重复。将宽度设置为与父项匹配。

    最后一个视图,设置为中间视图的右侧,宽度为包裹内容,并与父视图的右侧对齐。

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

  • 文件check\u info\u 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>
    
  • 以及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("\n");
                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();
        }
    }
    
    公共类CheckInfoTextView扩展了TextView{
    public CheckInfoTextView(上下文){
    超级(上下文);
    }
    public CheckInfoTextView(上下文、属性集属性){
    超级(上下文,attrs);
    }
    public CheckInfoTextView(上下文、属性集属性、int-defStyle){
    超级(上下文、属性、定义样式);
    }
    @凌驾
    WindowFocusChanged上的公共无效(布尔值hasWindowFocus){
    super.onWindowFocusChanged(hasWindowFocus);
    如果(!hasWindowFocus)返回;
    int requiredDots=getRequiredDotsNumber();
    如果(要求的点==0){
    字符串text=getText().toString();
    StringBuilder结果=新建StringBuilder();
    result.append(text.substring(0,text.lastIndexOf(“”));
    结果。追加(“\n”);
    result.append(text.substring(text.lastIndexOf(“”)+1));
    setText(result.toString());
    requiredDots=getRequiredDotsNumber();
    }
    串点=”;
    对于(int i=0;i

  • 我已经更改了Sergè的CheckinfoTextView类,这样它既可以在eclipse布局编辑器中工作,也可以在可能的情况下添加空格,以使页码尽可能靠近右侧。我也改变了它的使用方式

    完成:

    Milk...................23
    Chocolate cookies......24
    
    将文本分别设置为“牛奶23”和“巧克力饼干24”

    空格的数量是四舍五入到最近的,而不是向下四舍五入,因此最好将数字向右移一点,而不是向左移太多

    public class DotAutofillTextView extends TextView {
    
    private int availableWidthForDots;
    private int widthOfSpace;
    private int widthOfDotWithSpace;
    
    public DotAutofillTextView(Context context) {
        super(context);
    }
    
    public DotAutofillTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public DotAutofillTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int width = getWidth() - getPaddingLeft() - getPaddingRight();
        int lastLineWidth = (int) getLayout().getLineWidth(getLineCount() - 1);
        availableWidthForDots = width - lastLineWidth;
        int widthOfOneDot = getWidthOfOneDot();
        int widthOfTwoDotsWithSpace = getWidthOfTwoDotsWithSpace();
        widthOfSpace = widthOfTwoDotsWithSpace - (widthOfOneDot * 2);
        widthOfDotWithSpace = widthOfSpace + widthOfOneDot;
        int requiredDots = getRequiredDotsNumber();
        if (requiredDots != 0) {
            int spaces = getRequiredSpacesNumber(requiredDots);
            StringBuilder result = new StringBuilder();
            String text = getText().toString();
            result.append(text.substring(0, text.lastIndexOf(' ')));
            setText(result.toString());
            StringBuilder dots = new StringBuilder();
            for (int i = 0; i < requiredDots; ++i) {
                dots.append(" .");
            }
            for (int i = 0; i < spaces; ++i) {
                dots.append(" ");
            }
            result.append(dots.toString());
            result.append(text.substring(text.lastIndexOf(' ') + 1));
            setText(result.toString());
        }
        super.onLayout(changed, left, top, right, bottom);
    }
    
    private int getRequiredSpacesNumber(int requiredDots) {
        float remain = (1f * availableWidthForDots) % (1f * widthOfDotWithSpace);
        return (int) ((remain / widthOfSpace) + 0.5f);
    }
    
    private int getRequiredDotsNumber() {
        if (getLayout() == null) {
            return 1;
        }
        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();
    }
    
    公共类DotAutofillTextView扩展了TextView{
    专用int可从DTHFORDOTS获得;
    私人空间;
    与空间相交的私有int宽度;
    公共点自动填充文本视图(上下文){
    超级(上下文);
    }
    公共点自动填充文本视图(上下文、属性集属性){
    超级(上下文,attrs);
    }
    公共点自动填充文本视图(上下文上下文、属性集属性、int-defStyle){
    超级(上下文、属性、定义样式);
    }
    @凌驾
    仅限受保护的空心布局(布尔值已更改、整数左侧、整数顶部、整数右侧、整数底部){
    int width=getW
    
       <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
    
                <TextView
                    android:id="@+id/item"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Phone"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    />
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:text="...................................................................................................."
                    android:maxLines="1"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintLeft_toRightOf="@id/item"
                    app:layout_constraintRight_toLeftOf="@id/price"/>
    
                <TextView
                    android:id="@+id/price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="end"
                    android:text="100"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    />
       </androidx.constraintlayout.widget.ConstraintLayout>