1

有没有人有任何想法来增加或减少在 android.textview 上设置的文本中的字间距。我想通过提供任何整数值来做字间距和行间距...

4

2 回答 2

1

可能这会有所帮助

通过将此属性设置为您的 xml,您可以完成它

android:lineSpacingExtra 
于 2013-09-25T13:05:22.820 回答
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 回答