Androidのテキストビューに設定されたテキストの単語間隔を増減する考えはありますか? 任意の整数値を指定して単語間隔と行間隔を調整したい...
5497 次
2 に答える
1
こんにちは、要件に応じて単一のスペースを複数のスペースに置き換えることで、単語の間隔の問題を解決しました。1 つのテキストビューと 2 つのボタンがある例を挙げています。1 つはスペースを増やすためのもので、もう 1 つはスペースを減らすためのものです。コードは以下に示され、上記のように行間隔に android:lineSpacingExtra を使用しています。
package com.example.wordspacingexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView mTextView;
private Button mIncrease;
private Button mDecrease;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
mIncrease = (Button) findViewById(R.id.btn_increase);
mDecrease = (Button) findViewById(R.id.btn_decrease);
mTextView.setTag(" ");
mIncrease.setOnClickListener(this);
mDecrease.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_increase) {
String space = (String) mTextView.getTag();
String text = mTextView.getText().toString();
mTextView.setText(text.replace(space, (space += " ")));
mTextView.setTag(space);
} else if (v.getId() == R.id.btn_decrease) {
String space = (String) mTextView.getTag();
String text = mTextView.getText().toString();
if (space.length() > 2) {
mTextView.setText(text.replace(space,
space = space.substring(0, space.length() - 2)));
mTextView.setTag(space);
} else if (space.length() == 2) {
mTextView.setText(text.replace(space, " "));
mTextView.setTag(" ");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
これは、実装を伴うアクティビティのコードです。
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_increase"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Click to increase word space" />
<Button
android:id="@+id/btn_decrease"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_increase"
android:layout_centerHorizontal="true"
android:text="Click to decrease word space" />
使用される xml ファイルのコード。スクリーンショットは次のように提供されます。
于 2013-09-26T07:06:44.630 に答える