我是Android开发的新手,也是编程方面的新手。
我正在开发一个包含泰语在内的多种语言的Android应用程序。我的同事已经在整个应用程序中应用了letterSpacing,但我只能将其停用为泰语。你会如何实现这一目标?
我已尝试过以下解决方案,但它无效:
private boolean isThaiLanguage() {
return Locale.getDefault().getLanguage().equalsIgnoreCase("th");
}
private void applySpacing() {
if (this == null || this.originalText == null) return;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < originalText.length(); i++) {
builder.append(originalText.charAt(i));
if (i + 1 < originalText.length()) {
if (!isThaiLanguage()) {
builder.append("\u00A0");
}
}
}
SpannableString finalText = new SpannableString(builder.toString());
if (builder.toString().length() > 1) {
for (int i = 1; i < builder.toString().length(); i += 2) {
finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
super.setText(finalText, TextView.BufferType.SPANNABLE);
}
以下是我的同事成功申请letterSpacing的代码。
万分感谢;) 欣达 PS:如果您需要更多信息来解决这个问题,请告诉我。我很乐意提供帮助。
public class LetterSpacingButton extends Button {
public static final String TAG = "LetterSpacingButton";
private float spacing = Spacing.NORMAL;
private CharSequence originalText = "";
private TextView text;
public LetterSpacingButton(Context context) {
super(context);
}
public LetterSpacingButton(Context context, AttributeSet attrs) {
super(context, attrs);
initLetterSpacing(context, attrs);
}
public LetterSpacingButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initLetterSpacing(context, attrs);
}
private void initLetterSpacing(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.LetterSpacing, 0, 0);
try {
this.spacing = a.getFloat(R.styleable.LetterSpacing_letterSpacing, 0);
} finally {
a.recycle();
}
this.originalText = super.getText();
applySpacing();
}
public float getSpacing() {
return this.spacing;
}
public void setSpacing(float spacing) {
this.spacing = spacing;
this.originalText = super.getText();
applySpacing();
}
@Override
public void setText(CharSequence text, TextView.BufferType type) {
originalText = text;
applySpacing();
}
@Override
public CharSequence getText() {
return originalText;
}
private void applySpacing() {
if (this == null || this.originalText == null) return;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < originalText.length(); i++) {
builder.append(originalText.charAt(i));
if (i + 1 < originalText.length()) {
builder.append("\u00A0");
}
}
SpannableString finalText = new SpannableString(builder.toString());
if (builder.toString().length() > 1) {
for (int i = 1; i < builder.toString().length(); i += 2) {
finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
super.setText(finalText, TextView.BufferType.SPANNABLE);
}
public class Spacing {
public final static float NORMAL = 0;
}
}
第二档:
public class LetterSpacingTextView extends TextView {
private float spacing = Spacing.NORMAL;
private CharSequence originalText = "";
private boolean isQuickStart1 = false;
public LetterSpacingTextView(Context context) {
super(context);
}
public LetterSpacingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initLetterSpacing(context, attrs);
}
public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
initLetterSpacing(context, attrs);
}
private void initLetterSpacing(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.LetterSpacing, 0, 0);
try {
this.spacing = a.getFloat(R.styleable.LetterSpacing_letterSpacing, 0);
} finally {
a.recycle();
}
this.originalText = super.getText();
initLetterSpacing(context, attrs);
applySpacing();
}
}
public float getSpacing() {
return this.spacing;
}
public void setSpacing(float spacing) {
this.spacing = spacing;
this.originalText = super.getText();
applySpacing();
}
@Override
public void setText(CharSequence text, BufferType type) {
originalText = text;
if(isQuickStart1){
applySpacingQS1();
}else{
applySpacing();
}
}
@Override
public CharSequence getText() {
return originalText;
}
private void applySpacing() {
if (this == null || this.originalText == null) return;
StringBuilder builder = new StringBuilder();
for(int i = 0; i < originalText.length(); i++) {
builder.append(originalText.charAt(i));
if(i+1 < originalText.length()) {
builder.append("\u00A0");
}
}
SpannableString finalText = new SpannableString(builder.toString());
if(builder.toString().length() > 1) {
for(int i = 1; i < builder.toString().length(); i+=2) {
finalText.setSpan(new ScaleXSpan((spacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
super.setText(finalText, BufferType.SPANNABLE);
}
private void applySpacingQS1() {
if (this == null || this.originalText == null) return;
StringBuilder builder = new StringBuilder();
for(int i = 0; i < originalText.length(); i++) {
builder.append(originalText.charAt(i));
if(i+1 < originalText.length()) {
builder.append("\u00A0");
}
}
}
SpannableString finalText = new SpannableString(builder.toString());
if(builder.toString().length() > 1) {
for(int i = 1; i < builder.toString().length(); i+=2) {
finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int indexNote = builder.toString().indexOf("T H E N O T E S");
if(indexNote!=-1){
finalText.setSpan(new StyleSpan(Typeface.BOLD), indexNote, indexNote+"T H E N O T E S".length(), 0);
}
}
}
super.setText(finalText, BufferType.SPANNABLE);
}
public class Spacing {
public final static float NORMAL = 0;
public final static float STANDARD = -1;
}
public void setIsQuickStart1(Boolean isQuickStart1) {
this.isQuickStart1 = isQuickStart1;
}
}
EXAMPLE OF THE IMPLEMENTATION OF LETTERSPACING IN LAYOUT:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="32dp"
android:paddingStart="32dp"
android:paddingRight="32dp"
android:paddingEnd="32dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.smartsoftasia.perfume.view.activity.RegisterActivity">
<com.smartsoftasia.perfume.view.component.LetterSpacingTextView
android:layout_marginTop="32dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/perfumist"
android:id="@+id/textView7"
android:textStyle="bold"
custom:letterSpacing="3"
android:textSize="20sp"
android:textColor="@color/accent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<com.smartsoftasia.perfume.view.component.LetterSpacingTextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/perfume_advisor"
custom:letterSpacing="1"
android:textSize="10sp"
android:id="@+id/textView8"
android:textColor="@color/black_secondary_text"
android:layout_below="@+id/textView7"
android:layout_centerHorizontal="true"/>
<com.smartsoftasia.perfume.view.component.LetterSpacingTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
custom:letterSpacing="1"
android:text="@string/register"
android:textStyle="bold"
android:textColor="@color/black_text"
android:id="@+id/textView18"
android:layout_below="@+id/textView8"
android:layout_centerHorizontal="true"/>
<EditText
style="@style/edit_text_grey"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText_activity_register_email"
android:textSize="11sp"
android:hint="@string/hint_your_password"
android:layout_below="@+id/textView18"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
style="@style/edit_text_grey"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText_activity_register_fist_name"
android:textSize="11sp"
android:hint="@string/hint_fist_name"
android:layout_below="@+id/editText_activity_register_email"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
style="@style/edit_text_grey"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText_activity_register_last_name"
android:textSize="11sp"
android:hint="@string/hint_last_name"
android:layout_below="@+id/editText_activity_register_fist_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Spinner
style="@style/edit_text_grey"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText_activity_register_gender"
android:textSize="11sp"
android:hint="@string/hint_gender"
android:layout_below="@+id/editText_activity_register_last_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
style="@style/edit_text_grey"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText_activity_register_city"
android:textSize="11sp"
android:hint="@string/hint_city"
android:layout_below="@+id/editText_activity_register_gender"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
style="@style/edit_text_grey"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="date"
android:ems="10"
android:id="@+id/editText_activity_register_date_of_birth"
android:textSize="11sp"
android:hint="@string/hint_date_of_birth"
android:layout_below="@+id/editText_activity_register_city"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="false"
android:singleLine="true"/>
<EditText
style="@style/edit_text_grey"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_marginTop="8dp"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText_activity_register_password"
android:textSize="11sp"
android:hint="@string/hint_password"
android:layout_below="@+id/editText_activity_register_date_of_birth"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
style="@style/edit_text_grey"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText_activity_register_confirm_password"
android:textSize="11sp"
android:hint="@string/hint_confirm_password"
android:layout_below="@+id/editText_activity_register_password"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<com.smartsoftasia.perfume.view.component.LetterSpacingButton
style="@style/button_accent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/register"
android:textSize="16sp"
custom:letterSpacing="1"
android:id="@+id/button_activity_register_register"
android:layout_below="@+id/editText_activity_register_confirm_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"/>
<com.smartsoftasia.perfume.view.component.LetterSpacingButton
style="@style/button_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:textSize="14sp"
android:id="@+id/button_activity_register_cancel"
android:layout_below="@+id/button_activity_register_register"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:0)
对于任何想要在字母之间应用字母间距并希望将其删除特定语言或任何其他文本视图的人来说,这是解决方案:
LetterSpacingTextView.java:
private boolean isThaiLanguage() {
return Locale.getDefault().getLanguage().equalsIgnoreCase("th") ;
}
private void applySpacingQS1() {
if (this == null || this.originalText == null) return;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < originalText.length(); i++) {
builder.append(originalText.charAt(i));
if (i + 1 < originalText.length()) {
if (!isThaiLanguage()) builder.append("\u00A0");
}
}
SpannableString finalText = new SpannableString(builder.toString());
if (builder.toString().length() > 1)
{
for (int i = 1; i < builder.toString().length(); i += 2) {
if (!isThaiLanguage()) {
finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int indexNote = builder.toString().indexOf("T H E N O T E S");
if (indexNote != -1) {
finalText.setSpan(new StyleSpan(Typeface.BOLD), indexNote,
indexNote + "T H E N O T E S".length(), 0);
}
}
}
}
super.setText(finalText, BufferType.SPANNABLE);
}
LetterSpacingButton.java:
private boolean isThaiLanguage() {
return Locale.getDefault().getLanguage().equalsIgnoreCase("th") ;
}
private void applySpacing() {
if (this == null || this.originalText == null) return;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < originalText.length(); i++) {
builder.append(originalText.charAt(i));
if (i + 1 < originalText.length()) {
if (!isThaiLanguage()) {
builder.append("\u00A0");
}
}
}
SpannableString finalText = new SpannableString(builder.toString());
if (builder.toString().length() > 1) {
for (int i = 1; i < builder.toString().length(); i += 2) {
if (!isThaiLanguage()) {
finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
super.setText(finalText, TextView.BufferType.SPANNABLE);
}