android中Button显示两行字体(spannable)

package com.example.spannabletest;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.Gravity;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Button mButton = new Button(this);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mButton.setLayoutParams(lp);

		SpannableString ss1 = new SpannableString("支付宝\n快捷支付");
		//第一个参数是设置字体大小 true代表用dip单位,接下来是下标,最后是输入时的字符是否使用同样的样式,这里是都不需要。根本没输入
		ss1.setSpan(new AbsoluteSizeSpan(15, true), 0, 3,
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		ss1.setSpan(new AbsoluteSizeSpan(8, true), 4, 8,
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		mButton.setText(ss1);
		mButton.setGravity(Gravity.CENTER);
		addContentView(mButton, lp);
	}
}

你可能感兴趣的:(学习日记)