本文介紹了在文本視圖中添加圓點,就像在收據中一樣的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想在收據中實現類似產品列表的東西:
啤酒.20
牛奶.10
帶果醬的曲奇…………………15
智能手機10 GB 3 GHz
1 GB內存NFC 10MPx
攝像機..400
說明:
檢查信息(啤酒、牛奶)我認為它應該是TextView,我需要用圓點填充。
Money(20,10)是另一個應與ViewGroup右側對齊的TextView。
有沒有辦法做到這一點?也許我需要從TextView繼承并重寫onDraw()或其他什么?
非常感謝您的建議!
推薦答案
我有解決方案。也許它會對某人有所幫助。
文件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>
填寫信息字段的代碼(在活動中):
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);
和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();
}
}
這篇關于在文本視圖中添加圓點,就像在收據中一樣的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,