向TextView添加点

时间:2013-06-29 06:08:50

标签: android

我想实现像check(check)info这样的东西,例如:
啤酒......................................... 20
牛奶.......................................... 10
果酱饼干..................... 15
智能手机10GB 3GHz
1GB RAM NFC 10MPx
相机.................................. 400

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

任何想法如何做到这一点?也许我需要从TextView继承并覆盖onDraw()或其他东西?
非常感谢建议!!!

7 个答案:

答案 0 :(得分:3)

你应该为每一行创建三个TextViews,一个接一个地排列:

  1. TextView带产品名称(例如'啤酒')
  2. 带点的TextView
  3. 带有数字的TextView(例如'20')
  4. 然后将一个产品放在一行中。 Row应该是相对布局,其中:

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

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

答案 1 :(得分:3)

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

  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("\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();
        }
    }
    

答案 2 :(得分:2)

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

完成:

Milk...................23
Chocolate cookies......24

将文字设置为&#39; Milk 23&#39;和巧克力饼干24&#39;分别

空格的数量四舍五入到最近而不是向下舍入,所以最好将数字稍微偏向右边而不是向左边过多

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();
}

}

答案 3 :(得分:0)

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

我的建议,只使用我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);

这是一个带有硬编码值的示例,您可以尝试动态添加...

希望这会有所帮助......

答案 4 :(得分:0)

enter image description here您只能使用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>

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

答案 5 :(得分:0)

使用相对布局:

TextView View TextView

第一个设置宽度以包装内容,并设置在上一行的文本视图下方 第二个视图,设置在第一个文本视图的右侧,并使用带有水平重复点的背景可绘制。设置宽度以匹配父级 最后一个视图,设置在中间视图和宽度的右侧以包装内容并与父级右侧对齐。

答案 6 :(得分:0)

我遇到了同样的问题,并使用ConstraintLayout找到了该解决方案:

   <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>
相关问题